jQuery:TAB 键的 keyup?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4762594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: keyup for TAB-key?
提问by qwerty
So, i'm having some trouble getting my script to run IF the Tab key is pressed. After some quick googling, the charcode for Tab is 9. Also, while we're talking, is there any better ways of checking if a key is pressed without using charcodes? I'm asking because i keep getting the following Warning by firebug when using charcode:
所以,如果按下 Tab 键,我在让我的脚本运行时遇到了一些麻烦。经过一些快速的谷歌搜索,Tab 的字符代码是 9。另外,在我们说话的时候,有没有更好的方法来检查一个键是否被按下而不使用字符代码?我问是因为我在使用字符代码时不断收到以下警告:
The 'charCode' property of a keyup event should not be used. The value is meaningless.
不应使用 keyup 事件的 'charCode' 属性。价值是没有意义的。
Anyway, it still works, so that's not the problem. This is the code i use:
无论如何,它仍然有效,所以这不是问题。这是我使用的代码:
$('/* my inputs */').keyup(function(e) {
console.log('keyup called');
var code = e.keyCode || e.which;
if (code == '9') {
console.log('Tab pressed');
}
});
Using the above code the console is left blank, nothing is added (using Firebug). Of course i've tried actually doing stuff instead of logging text, but nothing executes. So can anyone see why this isn't working? Is there a better way of checking if a key is pressed?
使用上面的代码,控制台留空,没有添加任何内容(使用 Firebug)。当然,我试过实际做一些事情而不是记录文本,但没有执行任何操作。那么有人能明白为什么这不起作用吗?有没有更好的方法来检查是否按下了某个键?
Thanks in advance.
提前致谢。
回答by Chris Ladd
My hunch is that when you press the tab key, your form's input loses focus, before the keyup happens. Try changing the binding to the body, like this:
我的预感是,当您按下 Tab 键时,表单的输入会在 keyup 发生之前失去焦点。尝试更改对正文的绑定,如下所示:
$('body').keyup(function(e) {
console.log('keyup called');
var code = e.keyCode || e.which;
if (code == '9') {
alert('Tab pressed');
}
});
Then, if that works (it does for me) try changing the binding to keyDown, instead, and return false. That way you can implement your own custom behavior.
然后,如果这行得通(对我来说确实如此),请尝试将绑定更改为 keyDown,然后返回 false。这样您就可以实现自己的自定义行为。
$('.yourSelector').keydown(function(e) {
console.log('keyup called');
var code = e.keyCode || e.which;
if (code == '9') {
alert('Tab pressed');
return false;
}
});
One of these two should work... if it's body, you could attach a keydown handler on body, then if it's tab, find the field with focus (I think there's function .hasFocus()?), and if it's the one you want, proceed and return false. Otherwise, let the browser do its thing.
这两个中的一个应该工作......如果它是身体,你可以在身体上附加一个keydown处理程序,然后如果它是选项卡,找到具有焦点的字段(我认为有函数.hasFocus()?),如果它是你的想要,继续并返回false。否则,让浏览器做它的事情。
If you wanted to update the field WITH a tab character, try this in your callback:
如果您想使用制表符更新字段,请在您的回调中尝试以下操作:
var theVal = $(this).val();
theVal = theVal + ' ';
$(this).val(theVal);
Haven't tested it, but it should work. You could also append 3 or 4 spaces instead of the tab character, if it's giving you trouble.
还没有测试过,但它应该可以工作。如果给您带来麻烦,您还可以附加 3 或 4 个空格而不是制表符。
回答by AlanFoster
e = e || window.event;
if(e.keyCode == 9)
alert("Tab pressed");
Try this
尝试这个
回答by Karl Rosaen
if nothing happens no matter what key you press, perhaps there is something wrong with the selector you are using? Can you verify you are selecting properly, perhaps with something like:
如果无论您按什么键都没有反应,那么您使用的选择器可能有问题?你能确认你选择正确吗,也许是这样的:
console.log($('/* my inputs */').length);