elliott.david 2008-11-19 14:59 采纳率: 0%
浏览 897
已采纳

事件 Keypress: 按了哪个键?

With jQuery, how do I find out which key was pressed when I bind to the keypress event?

$('#searchbox input').bind('keypress', function(e) {});

I want to trigger a submit when <kbd>ENTER</kbd> is pressed.

[Update]

Even though I found the (or better: one) answer myself, there seems to be some room for variation ;)

Is there a difference between keyCode and which - especially if I'm just looking for <kbd>ENTER</kbd>, which will never be a unicode key?

Do some browsers provide one property and others provide the other one?

转载于:https://stackoverflow.com/questions/302122/jquery-event-keypress-which-key-was-pressed

  • 写回答

23条回答 默认 最新

  • YaoRaoLov 2008-11-19 15:07
    关注

    Actually this is better:

     var code = e.keyCode || e.which;
     if(code == 13) { //Enter keycode
       //Do something
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(22条)

报告相同问题?