问题
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
- KeyUp for tab button
- 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