javascript event.keyCode 不适用于 FireFox 3.6.25 中的 keydown、keypress 或 keyup 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8610663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript event.keyCode doesn't work on keydown, keypress or keyup events in FireFox 3.6.25
提问by Dougie
Please someone help a very desperate and weary novice. I have searched everywhere for an answer but to no avail.
请有人帮助一个非常绝望和疲惫的新手。我到处寻找答案,但无济于事。
I want to capture the keyCode/charCode to test whether the enter key has been pressed (I have been using keydown but I'm not fussed as long as I get something working). I have tried every conceivable solution and it works fine in IE, Safari, Chrome and Opera but NOT IN FireFox 3.6.25 (the latest)?! I know the event is definitely triggering as I can display a simple alert box from the function I call, but as for declaring a variable to hold the contents of event.keyCode, that's another matter entirely!
我想捕获 keyCode/charCode 来测试是否已按下 Enter 键(我一直在使用 keydown 但只要我得到一些工作,我就不会大惊小怪)。我已经尝试了所有可能的解决方案,它在 IE、Safari、Chrome 和 Opera 中运行良好,但不适用于 FireFox 3.6.25(最新)?!我知道事件肯定会触发,因为我可以从我调用的函数中显示一个简单的警报框,但是至于声明一个变量来保存 event.keyCode 的内容,那完全是另一回事!
The code of my form is basically...
我的表单的代码基本上是...
<form method="post" name="giftaidform" onsubmit="javascript:return validateForm();" action="../scripts/form-to-email.php">
<table width="500" border="0" align="center" cellpadding="5" cellspacing="0">
<tr>
<td width="180" align="right">Forename:</td>
<td align="left"><input name="forename" type="text" size="30" maxlength="30" onfocus="javascript:setUnchecked(this);" onkeydown="javascript:testForEnter(this);" onblur="javascript:isValidName(this);"></td>
</tr>
and so on...
等等...
As for my "testForEnter" function...
至于我的“testForEnter”功能......
function testForEnter(obj) {
// store code of key that was pressed
var keyCode = obj.keyCode;
alert(keyCode);
// if the enter key was pressed then prevent default action
if (keyCode == 13) {
if(event.preventDefault) event.preventDefault();
event.returnValue = false;
}
}
I have tried various other formulations including obj.charCode and stuff like below?!
我已经尝试了各种其他的公式,包括 obj.charCode 和类似下面的东西?!
function checkKey(evt) {
var keyID = (window.event) ? window.event.keyCode : event.which;
alert(keyID);
}
Nothing works in FireFox!
在 Firefox 中没有任何效果!
回答by Felix Kling
Nothing works because you named the parameter evt
and not event
. In your code, window.event
and event
are both referring to window.event
, which does not exist in Firefox.
没有任何作用,因为您命名了参数evt
而不是event
. 在您的代码中,window.event
和event
都指的是window.event
,它在 Firefox 中不存在。
You should normalize the event first, for example using
您应该先规范化事件,例如使用
var event = evt || window.event;
or rename the parameter from evt
to event
(and then drop the var
above).
或将参数从evt
to重命名event
(然后删除var
上面的内容)。
Also note that when you call a function from your inline event handler, you have to pass the event object to it if you want to access it.
另请注意,当您从内联事件处理程序调用函数时,如果您想访问它,则必须将事件对象传递给它。
回答by Dougie
This is the code that I now have and it is fully operational in all five main browsers:
这是我现在拥有的代码,它可以在所有五个主要浏览器中完全运行:
I now call the functions as follows:
我现在调用函数如下:
<input name="forename" type="text" size="30" maxlength="30" onkeydown="testForEnter(this,event);" onblur="isValidName(this);">
My function is as follows:
我的功能如下:
// testForEnter() disables the enter keypress on all form objects except for submit button
// this function works in IE, chrome, safari, opera and firefox
function testForEnter(obj,evt) {
var keyCode = evt.keyCode;
var objType = obj.type;
if ((keyCode == 13) && (objType != "submit")) {
evt.returnValue = false;
evt.stopPropagation(); // this line added for firefox
if(evt.preventDefault) evt.preventDefault();
}
} //end of testForEnter()
Hope this helps somebody else!
希望这对其他人有帮助!
回答by Geday
Try using
尝试使用
var keycode = evt.charCode;
It seems to work in both browsers.
它似乎适用于两种浏览器。