jquery - “点击”在按键上?

发布于 2024-10-10 01:53:16 字数 354 浏览 0 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晌融 2024-10-17 01:53:16

您不必“假装”点击。确保您的“输入”按钮是:

<input type="submit">

或者

<button type="submit">

并且它应该在按输入键时提交表单。

另一个潜在的问题是 Ajax 代码绑定到按钮的单击事件,而不是表单的提交事件。

$('button').click(...); // is wrong
$('form').submit(...); // is right 

You shouldnt have to "fake" a click. Make sure your "enter" button is either:

<input type="submit">

Or

<button type="submit">

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.

$('button').click(...); // is wrong
$('form').submit(...); // is right 
诺曦 2024-10-17 01:53:16

使用不带参数的 .click()

Use .click() with no parameters

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文