最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - event.which and event.keycode is not working on internet explorer 10 for backspace button - Stack Overflow

programmeradmin1浏览0评论

I have to track the alphabet keys and backspace key with JavaScript. I am using below written code to track every key press but unfortunately when i press backspace button in IE i gets nothing any idea regarding the issue.

    $('#crossword').delegate('.grid_input_holder','keypress',function(event){
            alert('keycode : '+event.keycode+' which : '+$(this).val( String.fromCharCode(event.which));
    });

Thanks in advance

I have to track the alphabet keys and backspace key with JavaScript. I am using below written code to track every key press but unfortunately when i press backspace button in IE i gets nothing any idea regarding the issue.

    $('#crossword').delegate('.grid_input_holder','keypress',function(event){
            alert('keycode : '+event.keycode+' which : '+$(this).val( String.fromCharCode(event.which));
    });

Thanks in advance

Share Improve this question edited Apr 7, 2014 at 7:04 keshu_vats asked Sep 8, 2013 at 10:13 keshu_vatskeshu_vats 4426 silver badges45 bronze badges 1
  • 1 possible duplicate of Javascript e.keyCode doesn't catch Backspace/Del in IE – raina77ow Commented Sep 8, 2013 at 10:16
Add a ment  | 

3 Answers 3

Reset to default 4

If you want to support IE and you use special keys (like delete and backspace) I suggest using keydown/keyup instead.

Special keys

Explorer doesn't fire the keypress event for delete, end, enter, escape, function keys, home, insert, pageUp/Down and tab.

If you need to detect these keys, do yourself a favour and search for their keyCode onkeydown/up, and ignore both onkeypress and charCode.

You can read more on cross browser issues of Detecting keystrokes (Quirksmode).

OR

Key Code for Backspace will take the value = 83 if we already have a few characters in a Text Box .

The Key Code will be = 8 if there are NO Characters in the Text Box and we are trying to Hit Backspace.

If you want to support IE and you use special keys (like delete and backspace) I suggest using keydown/keyup instead.

The problem araises when you are using IE10 . Because in the IE versions < 9 give

event.which == undefined || event.which == zero 

when special handling key is pressed like Backspace,DEL,INS,HOME,Navigation Keys, Enter, etc. So we could easily identify when a special handling character is pressed and can be handled seperately.

But in IE10 . event.which doesn't give undefined or zero for special handling keys instead it gives keycode.... Surprising thing is , It gives same key code for Dot( . ) and Delete , Single quote( ' ) and Right arrow , etc.

Hence we couldn't identify exactly in IE10 when special handling key is pressed by using neither event.which nor event.keycode .

In such Situations ... Use

event.charCode == 0 

For identifying special handling characters in IE10

Trust me friends, It saved me more than a week's effort.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论