Ajax submitHandler没有执行

时间:2016-08-17 13:25:46

标签: jquery ajax validation

我希望php文件通过ajax执行,但它只是通过表单正常发布并加载新页面。我在这里检查了大量代码,但它始终不通过submitHandler和ajax执行。我检查了主机提供程序并支持ajax,我已经使用jquery进行了其他测试,它工作正常。只是不能让它工作应该如何。 php文件执行perferectly并将数据插入数据库,所以我猜我错过了验证代码。

$("#emailForm").validate({

   submitHandler: function(form) {
//       event.preventDefault();
     $.ajax({
       url: $(form).attr("action"),
       type: $(form).attr("method"),
       data: $(form).serialize(),
       success: function(response) {
         document.getElementById("fee").style.display = "block";
       }
     });
     return false;
   }
 });
#fee {
  display: none;
}
<html>

<head>

  <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.0.min.js"></script>
  <script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.min.js"></script>

</head>

<body>
  <div class="screen">

    <div class="contents">
      <form class="cmxform" id="emailForm" method="post" action="process.php">
        <fieldset>
          <legend>Sign up now and secure your username!
          </legend>
          <p>
            <label for="cname">Username (required, at least 5 characters)
            </label>
            <input id="cname" name="name" minlength="5" type="text" required>
          </p>
          <p>
            <label for="cpassword">Password (required, at least 8 characters)
            </label>
            <input id="cpassword" name="password" minlength="8" type="password" required>
          </p>
          <p>
            <label for="cemail">Email (required)
            </label>
            <input id="cemail" type="email" name="email" required>
          </p>
          <p>
          </p>
          <p>
            <input class="submit" type="submit" value="Submit">
          </p>
        </fieldset>
      </form>
      <div id='fee'>Signup Complete!
      </div>
    </div>
  </div>
</body>

</html>

1 个答案:

答案 0 :(得分:1)

尝试使用,因为使用表单对象我不确定它是Javascript DOM对象还是jQuery对象所以我将对象转换为jQuery对象并使用attr()方法。

&#13;
&#13;
$(document).ready(function () {
$("#emailForm").validate({

   submitHandler: function(form) {
     $.ajax({
       url: $(form).attr("action"),
       type: $(form).attr("method"),
       data: $(form).serialize(),
       success: function(response) {
         document.getElementById("fee").style.display = "block";
       }
     });
     return false;
   }
 });
 });
&#13;
&#13;
&#13;