jQuery按键37问题,'%'和[左箭头]在IE 10中不起作用 [英] Jquery keypress 37 issue, '%' and [ left arrow ] not work in IE 10
问题描述
早上好
我在IE 10上遇到问题,我的按键仍然可以输入'%',但是FF和Chrome都没有此问题. 我发现键37是[左箭头],与ASCII中的'%'相匹配. 我的示例代码如下:
I facing a issue on the IE 10 where my keypress still can enter '%' but the FF and Chrome no such issue. I found out that the key 37 is the [ left arrow ] which match with '%' in ASCII. My sample code as below:
$('#refId').bind("keypress", function(event) {
// allow letters, numbers and keypad numbers ONLY
var key = event.charCode;
if((key >= 48 && key <= 57) ||
(key >= 65 && key <= 90) ||
(key >= 97 && key <= 122)){
return true;
}
//allow backspace, tab, left arrows, right arrow, delete
key = event.keyCode;
if(key == 8 ||
key == 9 ||
key == 37 ||
key == 39 ||
key == 46){
return true;
}
return false;
});
能告诉我一些解决方法吗?
Can give me some idea how to fix this?
谢谢. -fsloke
Thanks. -fsloke
推荐答案
改为使用var key = event.which;
并加入if语句.
Use var key = event.which;
instead and join the if-statements.
event.which属性规范化event.keyCode和event.charCode. 建议观看事件.该事件用于键盘键输入.
The event.which property normalizes event.keyCode and event.charCode. It is recommended to watch event.which for keyboard key input.
- https://api.jquery.com/event.which/
$('#refId').on("keydown", function(event) {
// allow letters, numbers and keypad numbers ONLY
var key = event.which;
if((key >= 48 && key <= 57) ||
(key >= 65 && key <= 90) ||
(key >= 97 && key <= 122) ||
key == 8 ||
key == 9 ||
key == 37 ||
key == 39 ||
key == 46) {
return true;
}
return false;
});
这篇关于jQuery按键37问题,'%'和[左箭头]在IE 10中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!