【问题标题】:Jquery Ajax Simple form annot get form to stop submitting with <Enter>Jquery Ajax 简单表单无法获取表单以停止使用 <Enter> 提交
【发布时间】:2016-02-13 03:38:46
【问题描述】:

这让我发疯了。 无论我做什么,只要您在任何表单字段中输入,我都无法阻止提交此 Ajax JQuery 表单。

我一直在寻找论坛,但似乎没有任何建议有帮助。我正在刷新我的浏览器窗口,我认为它会刷新所有代码,但这越来越疯狂。

请帮忙!

<title>555</title>
<!-- jQuery --> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<!-- The Ajax Submit Script -->
    <script>
      $(function () {


        $('#msform').submit(function(e){

          e.preventDefault();

          $.ajax({
            type: 'post',
            url: 'assets/upload.php',
            data: $('#msform').serialize(),
            success: function () {
              alert('form was submitted');
            }
          });
            return false;  
        });

      });   
    </script>


</head>
<body>


<!-- start error message div -->
<div id="form-messages"></div>
<!-- end error message div -->

<!-- multistep form -->
<form id="msform" name="msform">
    <fieldset>
        <h2 class="fs-title">Submit This</h2>
        <h3 class="fs-subtitle">heidiho neighbor!</h3>
        <input type="text" name="website" placeholder="Website" />
        <table>
            <tr>
                <td><input type="text" name="firstname" placeholder="First Name" /></td>
                <td><input type="text" name="lastname" placeholder="Last Name" /></td>
            </tr>
        </table>    
        <input type="text" name="email" placeholder="Email" />
        <input type="text" name="phone" placeholder="Phone" />
        <input type="submit" value="submit" id="submit"/>
    </fieldset>
</form>

【问题讨论】:

  • 当我测试它时,该代码成功地阻止了表单提交 - jsbin.com/guhuxi/1/edit?html,output - 虽然,显然,在 Ajax 响应返回后表明它已提交的警报将会触发。

标签: php jquery ajax forms


【解决方案1】:

我认为您需要捕获文本框上的 Enter 键并在找到时禁用它们。

$(":text").keydown(function(e){
    if(e.which==13)
    {
        return false;
    }
});

【讨论】:

  • 我真的很想弄清楚 preventdefault 演员。这可能有效,但 ti 会普遍减少功能。
  • preventDefault() 将停止元素的默认行为(如果有)。假设你有form,你想停止submit,那么如果你使用它,表单将不会被提交。如果您将其与超链接单击一起使用,则链接将不会浏览到另一个位置。所以对于文本框,据我所知这是个好主意
猜你喜欢
  • 2012-03-14
  • 2012-04-22
  • 1970-01-01
  • 1970-01-01
  • 2012-12-15
  • 1970-01-01
  • 2011-08-31
  • 2014-03-11
相关资源
最近更新 更多