jQuery KeyUp 和单击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5871206/
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 and Click
提问by Tom
I was wondering how you can do .keyup()
and .click()
for the same #id
?
我想知道你该怎么办.keyup()
,并.click()
为同#id
?
i.e. essentially I want to validate the #id
when both the user attempts to hit enteror hits the #search
button.
即基本上我想验证#id
用户何时尝试点击enter或点击#search
按钮。
Thanks alot
非常感谢
回答by Blender
$('#foo').bind('click keyup', function(event) {
...
You'll have to add some logic, as the event type changes, but it should work with enough if
blocks.
随着事件类型的变化,您必须添加一些逻辑,但它应该可以使用足够的if
块。
回答by Naftali aka Neal
Well you can do:
那么你可以这样做:
$(document).keydown(function(objEvent) {
if (objEvent.keyCode == 13) { //clicked enter
$('#search').click(); //do click
}
})
$("#search").click(function(e){/*click fn*/})
Will run the click on enter press
将运行点击回车按
回答by Mo Valipour
$("#id").click(validate).keyup(function(event)
{
if (event.keyCode == '13') validate();
});
function validate() { ... validate $(this).val(); ... }
回答by Aron Rotteveel
I'd go for something like this:
我会去做这样的事情:
function validate(element) {
// your validation stuff goes here
}
$('#id').keyup(function(event) {
if (event.keyCode == 13) {
validate(this);
}
}).click(function() {
validate(this);
});
回答by fidoogle
The new jQuery offers the which property on the event to check which key was pressed so yo can do something like this now:
新的 jQuery 提供了事件的 which 属性来检查按下了哪个键,所以你现在可以做这样的事情:
$(#id").on('keyup',function(e){
if (e.which==13 || e.which==9) doSomething(this); //Enter or Tab key
});