输入按钮无法在Google Chrome中使用 [英] Enter Button is not working in Google chrome

查看:112
本文介绍了输入按钮无法在Google Chrome中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按下回车按钮时,将启用提交按钮。下面的代码在IE中工作,但它不适用于谷歌浏览器





When I press enter button submit button will be enable . Below code is working in IE but its not working in google chrome


<script type="text/javascript">
     
      
                function doClick(Button1, e) {
                                var key;

                    if (window.event)
                        key = window.event.keyCode;     
                    else
                        key = e.which;     

                    if (key == 13) {
                
                        var btn = document.getElementById(Button1);
                        if (btn != null) { 
                            btn.click();
                            event.keyCode = 0
                        }
                    }
        }

       


</script>

推荐答案

你做错了。首先,调用键盘事件处理程序时只传递一个参数,而不是两个参数。因此,您的第二个实际参数是 undefined 。当您取消引用这个未定义的对象试图获取 e.which 时,会抛出异常并且可能没有被捕获,并且,无论如何,整个过程都不​​起作用。



此外,窗口对象没有属性event,具有类似的结果。尝试获取 window.event 也会返回 underfined



您需要使用只有一个参数的处理程序,这是一个事件对象。传递的实际对象将是 KeyboardEvent 对象,您可以使用该对象提取所需的所有事件属性。这里有很好的解释:

https://developer.mozilla。 org / zh-CN / docs / Web / API / KeyboardEvent [ ^ ],

http://javascript.info/教程/键盘事件 [ ^ ]。



-SA
You are doing it all wrong. First of all, keyboard event handlers are called with passing only one argument, not two. Therefore, your second actual argument is undefined. When you dereference this underfined object attempting to take e.which, an exception is thrown and probably not caught, and, anyway, the whole thing does not work.

Besides, window object does not have the property "event", with similar "results". An attempt to get window.event returns underfined, too.

You need to use a handler with only one argument, which is an event object. The actual object passed will be the KeyboardEvent object, which you can use to extract all the event properties you need. This is well explained here:
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent[^],
http://javascript.info/tutorial/keyboard-events[^].

—SA


您没有在按钮中选择点击事件...双击你的按钮并在那里写代码......
you did not select a on-click event in button... double click your button and write the code there...


这篇关于输入按钮无法在Google Chrome中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆