admin 管理员组文章数量: 1086019
I am able to successfully detect the Enter key in Internet Explorer 10, Chromium and Opera, but not in Firefox.
I have found a few pages on here, though they do not work in Firefox either. Am I doing something wrong, here?
TypeScript:
function HandleKeyPress(e) {
var key = e.keyCode || e.which;
if (key == 13) {
// We got this.
var textbox = <HTMLInputElement>document.getElementById("tbox");
sayHello(textbox.value);
}
}
Resulting JavaScript (identical):
function HandleKeyPress(e) {
var key = e.keyCode || e.which;
if(key == 13) {
var textbox = document.getElementById("tbox");
sayHello(textbox.value);
}
}
HTML:
<input type="text" value="dfgdfgdfg" id="tbox" onkeypress="HandleKeyPress(event)" />
I can't use any external libraries, I have to do this in pure JavaScript (or a language that piles to pure JS).
Update:
I just installed Firebug and in the Consolw pane I noticed (just after pressing the Enter jey on the textfield:
http://localhost:1058/Default.cshtml?someValue=dfgdfgdfgkjkhj
<p>You said: dfgdfgdfgkjkhj</p>
See below for the pic:
Notice that the page itself has not updated the value of the response
paragraph to what was entered in the textbox, but, Firebug does indeed say that it was successfully posted.
Now, since this code works on every other browser (IE, Chrome/Canary and <, Opera, Safari), my question is, maybe Firefox is detecting the key event, but the part that isn't working is after that?
I am able to successfully detect the Enter key in Internet Explorer 10, Chromium and Opera, but not in Firefox.
I have found a few pages on here, though they do not work in Firefox either. Am I doing something wrong, here?
TypeScript:
function HandleKeyPress(e) {
var key = e.keyCode || e.which;
if (key == 13) {
// We got this.
var textbox = <HTMLInputElement>document.getElementById("tbox");
sayHello(textbox.value);
}
}
Resulting JavaScript (identical):
function HandleKeyPress(e) {
var key = e.keyCode || e.which;
if(key == 13) {
var textbox = document.getElementById("tbox");
sayHello(textbox.value);
}
}
HTML:
<input type="text" value="dfgdfgdfg" id="tbox" onkeypress="HandleKeyPress(event)" />
I can't use any external libraries, I have to do this in pure JavaScript (or a language that piles to pure JS).
Update:
I just installed Firebug and in the Consolw pane I noticed (just after pressing the Enter jey on the textfield:
http://localhost:1058/Default.cshtml?someValue=dfgdfgdfgkjkhj
<p>You said: dfgdfgdfgkjkhj</p>
See below for the pic:
Notice that the page itself has not updated the value of the response
paragraph to what was entered in the textbox, but, Firebug does indeed say that it was successfully posted.
Now, since this code works on every other browser (IE, Chrome/Canary and <, Opera, Safari), my question is, maybe Firefox is detecting the key event, but the part that isn't working is after that?
Share Improve this question edited Oct 29, 2012 at 16:45 Arrow asked Oct 29, 2012 at 15:33 ArrowArrow 2,9248 gold badges41 silver badges61 bronze badges 13-
2
As far as I can tell, every version of Firefox will correctly set the
which
property of the event object in thekeypress
event handler. It won't always set thekeyCode
property, but that shouldn't matter since you check for either. Can you make a fiddle to demonstrate the issue? – James Allardice Commented Oct 29, 2012 at 15:39 -
Here's an example - focus the input and press any key to get an alert. The enter key should alert
true
. jsfiddle/YUQuU – James Allardice Commented Oct 29, 2012 at 15:41 - Thanks @JamesAllardice: I just tried a JSFiddle, but it wouldn't even work in IE on the fiddle site. :/ Here's the link anyway: jsfiddle/bMbUt/1 – Arrow Commented Oct 29, 2012 at 15:48
- @JamesKent - The fiddle doesn't work because the JS is included in the wrong place. Here's your fiddle again updated: jsfiddle/bMbUt/2 – James Allardice Commented Oct 29, 2012 at 15:52
- @JamesAllardice: WTH? I just checked your link (jsfiddle/bMbUt/2), and (in Firefox 15.0) it works! But when I use the exact same code in a diff page it doesn't. :/ That's weird. – Arrow Commented Oct 29, 2012 at 16:05
3 Answers
Reset to default 5You should change onkeypress
to onkeydown
as onkeypress
only fires for printable characters in some implementations.
Your existing code should work with onkeydown
.
try this:
<input type="text" value="dfgdfgdfg" id="tbox" onkeypress="HandleKeyPress(event)" />
And define the function HandleKeyPress
as:
function HandleKeyPress(evt){
var key = evt.which || evt.charCode || evt.keyCode || 0;
if (key == 13) ...
}
You can acplish by using jQuery like this
$(".input").keyup(function (e) {
if (e.keyCode == 13) {
// Whatever
}
});
本文标签: javascriptHow can I detect the Enter key in FirefoxStack Overflow
版权声明:本文标题:javascript - How can I detect the Enter key in Firefox? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744050966a2525010.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论