jquery + ie8:未检测到退格键?
我想在我的输入字段之一中禁用退格键,因此我编写了以下 jquery:
$("#noBackspacesHere").keypress(function(e){
if(e.which == "8"){
return false;
}
});
这在 Firefox 中工作正常,但在 ie8 中似乎不起作用。 我已经读到 .keyCode 有问题,所以这就是我选择 .which 的原因。
I want to disable backspace in one of my inputfields so I wrote the following jquery:
$("#noBackspacesHere").keypress(function(e){
if(e.which == "8"){
return false;
}
});
This works fine in firefox, but doesn't seem to work in ie8.
I've already read that .keyCode has issues so that's why I went for .which.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Jonathon Bolster 的代码不适用于 Opera。
这应该适用于 Opera、IE、FF 和 Chrome。
此外,
e.which
是检查关键代码的首选方法,因为 jQuery 规范了事件对象,因此无论您使用哪种浏览器,which
都将始终存在。@Jonathon Bolster's code doesn't work for Opera.
This should work in Opera, IE, FF and Chrome.
Also,
e.which
is the preferred way to check for key codes because jQuery normalizes the event object so thatwhich
will always be present no matter what browser you use.我刚刚玩弄了键盘事件,
keydown
会在这里帮助您:示例: http: //jsfiddle.net/jonathon/7xBRf/
我做了
typeof(e.which)
,它说它是一个数字,所以你不需要它周围的引号。我已经在 IE8、Chrome 和 FF 中对此进行了测试,它似乎对它们有效。I just played around with the keyboard events and
keydown
will help you here:Example: http://jsfiddle.net/jonathon/7xBRf/
I did
typeof(e.which)
and it said that it's a number, so you don't need the quotes around it. I've tested this in IE8, Chrome and FF and it seems to work for them.