如果 Enter 命中,则 jQuery 调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15802858/
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 call function if Enter hit
提问by PHP Ferrari
I am calling a function on button click code is given blow:
我正在调用按钮单击代码上的函数受到打击:
<input type="button" value="Search" id="go" />
$("#go").click(function ()
{
...
});
now I catch if user hit enterkey from keyboard by this function:
现在我抓住用户是否enter通过此功能从键盘上按键:
$("#s").keypress(function(e) {
if(e.which == 13) {
alert('You pressed enter!');
}
});
but how could I call
但我怎么能打电话
$("#go").click(function ()
{
...
});
both if user hits enterkey & on click GObutton?
如果用户点击enter键和点击GO按钮?
回答by Barmar
Trigger the click handler explicitly:
显式触发点击处理程序:
$("#s").keypress(function(e) {
if(e.which == 13) {
alert('You pressed enter!');
$("#go").click();
}
});
回答by csharp
you can use keyup
event :
您可以使用keyup
事件:
$("#s").keyup(function(e) {
if (e.which == 13) {
$("#go").click();
}
});
回答by karthick
Try this one:
试试这个:
$("#s").keypress(function(e) {
if(e.which == 13) {
e.preventDefault();
$("#go").click();
}
});
回答by Pir Abdul
Use both click event and mouse event: I affraid, you have not mention textbox there, so I suppose you do both on button.
使用单击事件和鼠标事件:我怕,你没有在那里提到文本框,所以我想你在按钮上都做了。
$("#go").keypress(function(e) {
//Event.which == 1 mouse click left and event. which == 13 is enter key.
if(e.which == 13 || e.which == 1 ) {
alert('You pressed enter or clicked left mouse');
}
});
});