How to trigger an event after hitting Tab key in textbox

喜夏-厌秋 提交于 2019-12-22 08:44:39

问题


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

<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 Tab it will do something.

Im using jquery1.7.2.min.js

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

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


回答1:


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

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

Fiddle Demo




回答2:


Will this help

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

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




回答3:


<input type="text" />

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



回答4:


$(document).ready(function() {

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

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

demo here..

http://jsfiddle.net/QfCpC/




回答5:


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

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




回答6:


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

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



回答7:


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

  1. KeyUp for tab button
  2. Blur action for Input type field

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

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

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


来源:https://stackoverflow.com/questions/14889732/how-to-trigger-an-event-after-hitting-tab-key-in-textbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!