jQuery 事件按键:按下了哪个键? [英] jQuery Event Keypress: Which key was pressed?
本文介绍了jQuery 事件按键:按下了哪个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 jQuery,如何在绑定到 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) {});
我想在按下 ENTER 时触发提交.
I want to trigger a submit when ENTER is pressed.
[更新]
即使我自己找到了(或更好的:一个)答案,似乎仍有一些变化的空间;)
Even though I found the (or better: one) answer myself, there seems to be some room for variation ;)
keyCode
和 which
之间有区别吗 - 特别是如果我只是在寻找 ENTER,它永远不会是一个 unicode 键?
Is there a difference between keyCode
and which
- especially if I'm just looking for ENTER, which will never be a unicode key?
某些浏览器是否提供一种属性而其他浏览器提供另一种属性?
Do some browsers provide one property and others provide the other one?
推荐答案
其实这样更好:
var code = e.keyCode || e.which;
if(code == 13) { //Enter keycode
//Do something
}
这篇关于jQuery 事件按键:按下了哪个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文