11

我需要在一些动态输入上捕获选项卡按键事件,但使用按键事件的正常语法似乎没有捕捉到关键代码。

$('input').live('keypress', function (e) {
   if ( e.which == 9 )
       alert( 'Tab pressed' );
});

无论我按哪个键,当我通过 firebug 中的调试器时,这似乎都将 0 捕获为按键。

4

2 回答 2

25

尝试使用 .keyCode 而不是 .which:

$('input').live('keypress', function (e) {
   if ( e.keyCode == 9 ){
       alert( 'Tab pressed' );
    }
});

似乎工作;)

于 2009-07-22T15:12:39.780 回答
9

尝试听keyupkeydown代替keypress根据此 SO 帖子

于 2009-07-22T15:09:27.587 回答