jquery + ie8: backspace not being detected?
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.
@Jonathon Bolster's code doesn't work for Opera.
This should work in Opera, IE, FF and Chrome.
$("#noBackspacesHere").bind("keypress keydown", function(e) {
if (e.which == 8) {
e.preventDefault();
}
});
Also, e.which
is the preferred way to check for key codes because jQuery normalizes the event object so that which
will always be present no matter what browser you use.
I just played around with the keyboard events and keydown
will help you here:
$("#noBackspacesHere").keydown(function(e) {
if (e.which == 8) {
return false;
}
});
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.
精彩评论