谁还没个明天 2008-11-19 14:59 采纳率: 50%
浏览 507
已采纳

事件 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条回答 默认 最新

  • 零零乙 2008-11-19 15:07
    关注

    Actually this is better:

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

报告相同问题?

悬赏问题

  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题