jquery - “点击”在按键上?
我有一个ajax搜索表单,如果您单击“提交”,输入邮政编码,单击“输入”按钮,则可以正常工作,然后立即弹出结果。我试图做到这一点,以便如果用户按键盘上的“enter”键,它会产生相同的效果。看起来正在提交,但没有结果......
我尝试过:
if($('#search').length) {
$('#zipcode').keyup(function(e) {
if(e.keyCode == 13) {
e.preventDefault();
$('#search').submit();
}
});
}
I have an ajax search form that works fine if you click "submit" in that you enter a zipcode, click the "Enter" button and up pops results instantly. I'm trying to make it so that if the user presses "enter" on the keyboard, it has the same effect. It looks like it's submitting, but no results come up...
I tried:
if($('#search').length) {
$('#zipcode').keyup(function(e) {
if(e.keyCode == 13) {
e.preventDefault();
$('#search').submit();
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不必“假装”点击。确保您的“输入”按钮是:
或者
并且它应该在按输入键时提交表单。
另一个潜在的问题是 Ajax 代码绑定到按钮的单击事件,而不是表单的提交事件。
You shouldnt have to "fake" a click. Make sure your "enter" button is either:
Or
And it should submit the form on pressing enter.
The other potential problem is that the Ajax code is bound to the button's click event, rather than the form's submit event.
使用不带参数的 .click()
Use .click() with no parameters