jQuery:如何使用live()捕获按键 [英] jQuery: how to capture keypress key using live()

查看:82
本文介绍了jQuery:如何使用live()捕获按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在一些动态输入上捕获一个tab键按下事件,但是使用keypress事件的常规语法似乎并没有捕获键代码.

I need to capture a tab keypress event on some dynamic inputs, but the normal syntax using the keypress event doesn't seem to be catching the key code.

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

无论我按哪个键,在Firebug中调试器时,这似乎都捕获了0作为按键.

This seems to be catching 0 as the keypress when I go through the debugger in firebug no matter which key I press.

推荐答案

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

Try it with .keyCode instead of .which:

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

似乎可以工作;)

这篇关于jQuery:如何使用live()捕获按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆