【问题标题】:Submit button isn't triggering form submit提交按钮未触发表单提交
【发布时间】:2013-09-02 20:05:36
【问题描述】:

我有这个 html 表单,它的提交按钮有一些奇怪的行为。

当我单击“提交”按钮时,它一直处于按下状态,但没有提交表单。但是,如果表单是通过其他方式提交的,比如在键盘上按 Enter,或者在 Mobile Safari (iDevice) 上按 GO,一切正常。我正在使用 jQuery 移动顺便说一句。

谁能明白为什么会这样?

HTML

<div class='login' id='login'>
    <div class='login-header'>Please Login</div>
    <div class='alert-error' id='login-error' style='display:none;'>
      Please check your username and password and try again.
    </div>

    <form id='login_form'>
      <br>
      <p class='label'>Username</p>
      <input type='text' name='username'>
      <p class ='label'>Password</p>
      <input type='password' name='password'>
      <button type='submit' data-theme='a' id='login-button'>Login</button> 
    </form>
  </div>

JS

$('#login_form').submit(function(e) {
        e.preventDefault();
        e.stopPropagation();

        var serializedFormData = $(this).serialize();

        // blah blah ajax form submit
        });
    });

编辑

看来我可以通过切换以下行来解决我的问题:

<button type='submit' data-theme='a' id='login-button'>Login</button> 

<input type='submit' value='Submit'>.

任何知道这为什么奏效的人仍有待获得声誉。

【问题讨论】:

  • 不,按钮标签完全没问题。

标签: javascript jquery html forms jquery-mobile


【解决方案1】:

尝试删除 e.preventDefault();看看按钮是否工作。

【讨论】:

  • 不,事实上这会导致表单中断。
【解决方案2】:

试试

  $('#login_button').click(function(e) {
    e.preventDefault();

    var serializedFormData = $('#login_form').serialize();

    // blah blah ajax form submit
    });
});

这就是我使用 ajax 提交表单的方式,按下回车键仍然可以正常工作

【讨论】:

    【解决方案3】:

    没有&lt;button type="submit"&gt;Submit me&lt;/button&gt;

    提交按钮实际上应该是带有type="submit"input标签

    【讨论】:

    • 这显然是错误的。有一个
    • 是的,您的链接似乎有效。无论如何,这没有回答,为什么它像错误一样工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 2014-08-09
    • 2023-04-10
    相关资源
    最近更新 更多