jQuery 在文本框中按 Tab 键后如何触发事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14889732/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:02:22  来源:igfitidea点击:

How to trigger an event after hitting Tab key in textbox

tabsjquery

提问by TheOnlyIdiot

I want to trigger an event like displaying an alert message when I hit the Tabkey inside a textbox.

我想触发一个事件,比如当我点击Tab文本框中的键时显示警报消息。

<input type="text" />

$("input").blur(function (e) {
   if (e.which == 9)
       alert("Hurray!!!");
});

What I want to happen is that whenever I type inside a textbox then hit Tabit will do something.

我想要发生的是,每当我在文本框中输入然后点击Tab它就会做一些事情。

Im using jquery1.7.2.min.js

我正在使用 jquery1.7.2.min.js

I really don't know if Im doing it right.

我真的不知道我做得对不对。

For the demo http://jsfiddle.net/QfCpC/

对于演示http://jsfiddle.net/QfCpC/

回答by Raab

$("input").keydown(function (e) {

   if (e.which == 9)
       alert("Hurray!!!");
});

Fiddle Demo

小提琴演示

回答by Rahul R.

<input type="text" />

$("input").keydown(function (e) {
   if (e.which == 9)
        $('#someButton).trigger('click');//or you can directly call the handler also
});

回答by Neel

$(document).ready(function() {

    $("input").bind("keydown",function (e) {

   if (e.which == 9)        
       alert("Hurray!!!");
});
});

demo here..

演示在这里..

http://jsfiddle.net/QfCpC/

http://jsfiddle.net/QfCpC/

回答by Moons

Will this help

这会有所帮助吗

$("input").live("keydown" , function (e) {
if (e.which == 9)
   alert("Hurray!!!");
});

http://jsfiddle.net/QfCpC/3/

http://jsfiddle.net/QfCpC/3/

回答by Anujith

Try : http://jsfiddle.net/cEzLL/

试试:http: //jsfiddle.net/cEzLL/

$("input").keydown(function (e) {
   if (e.keyCode === 9)
       alert("Hurray!!!");
});

回答by Sahil Grover

The reason is when u hit 'tab' two actions takes place

原因是当你点击“标签”时会发生两个动作

  1. KeyUp for tab button
  2. Blur action for Input type field
  1. 选项卡按钮的 KeyUp
  2. 输入类型字段的模糊操作

Now according to your code you are adding eventlistner to blur event ... and blur event doesn't have property of giving you key binding.

现在根据您的代码,您正在将 eventlistner 添加到 blur 事件中……而 blur 事件没有为您提供键绑定的属性。

So in order to do this you need to bind "keydown".

所以为了做到这一点,你需要绑定“keydown”。

$("input").keydown(function (e) {
  if (e.which == 9)
       alert("YEYYYYYYY!!!");
});

回答by Phil Bozak

In order for the e.whichparameter to be set properly, I believe it has to be called from a keydown event.

为了e.which正确设置参数,我认为必须从 keydown 事件中调用它。

See the fiddle here. http://jsfiddle.net/QfCpC/2/

请参阅此处的小提琴。http://jsfiddle.net/QfCpC/2/