admin管理员组文章数量:1433956
I've the following jquery function which avoid typing "1" in the input text:
<input id="myId" style="width:50%;" value="0" type="text">
$("#myId").keypress(function(e) {
var x=e.charCode;
if (x == '49'){
e.preventDefault();
}
});
This is the jsfiddle
which is not working on IE8. Where is my error? Thanks in advance!
EDIT JAVASCRIPT VERSION
<input id="myId" style="width:50%;" value="0" type="text" onkeypress="javascript:checkNumber(event);"/>
function checkNumber(event) {
var x=e.which;
if (x == 49){
event.preventDefault();
}
}
I've the following jquery function which avoid typing "1" in the input text:
<input id="myId" style="width:50%;" value="0" type="text">
$("#myId").keypress(function(e) {
var x=e.charCode;
if (x == '49'){
e.preventDefault();
}
});
This is the jsfiddle
which is not working on IE8. Where is my error? Thanks in advance!
EDIT JAVASCRIPT VERSION
<input id="myId" style="width:50%;" value="0" type="text" onkeypress="javascript:checkNumber(event);"/>
function checkNumber(event) {
var x=e.which;
if (x == 49){
event.preventDefault();
}
}
Share
Improve this question
edited Nov 21, 2012 at 15:13
Fseee
asked Nov 21, 2012 at 14:21
FseeeFseee
2,6099 gold badges42 silver badges68 bronze badges
1
- 1 Mandatoried link: unixpapa./js/key.html – Florian Margaine Commented Nov 21, 2012 at 14:30
3 Answers
Reset to default 3charCode
is not cross-browser, you can use jQuery which
property:
The
event.which
property normalizesevent.keyCode
andevent.charCode
. It is remended to watchevent.which
for keyboard key input.
var x = e.which;
If you don't use jQuery, you can code:
var x = e.charCode || e.keyCode;
Use e.which
instead, since e.charCode
isn't supported on all browsers.
Also, you might consider using .keyup()
event instead of .keypress()
See this answer to a similar question. Try keydown
instead of keypress
and keyCode
instead of charCode
:
$("#myId").keydown(function(e) {
var x=e.keyCode;
if (x == '49'){
e.preventDefault();
}
});
the jQuery documentation for keypress lists a few good reasons to use keydown
instead, not the least of which is that it only fires once for a press and hold, and keypress reports the character, not the key (which is problematic when dealing with things like capslock.)
本文标签: jQuery javascript keypress function not working on IE8Stack Overflow
版权声明:本文标题:jQuery javascript keypress function not working on IE8 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745621187a2666690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论