jQuery Validate:submitHandler提交表单而不点击form.submit

时间:2013-08-05 19:09:38

标签: jquery jquery-plugins jquery-validate

jQuery Validate正在运行(当所需的电子邮件字段为空时触发错误)但未触发submitHandler中的代码(警报未显示)。由于submitHandler无法正常工作,因此我不希望将表单发布到secure / process_login.php,因为form.submit();在不工作的alert()之后,但是如果它有效则它会发生。

表格代码:

<form action="secure/process_login.php" method="post" id="login_form" name="login_form">
  <fieldset>
    <legend>Log In</legend>
    <div class="form-group">
      <label for="inputEmail">Email address</label>
      <input type="text" id="email" class="form-control" name="email"placeholder="Your Email Address" required>
    </div>
    <div class="form-group">
      <label for="inputPassword">Password</label>
      <input type="password" class="form-control" name="password" id="password" placeholder="Your Password">
      <input type="hidden" name="p" id="p" value="">
    </div>
    <button type="submit" class="btn btn-primary form-control">Log In</button>
  </fieldset>
</form>

脚本:

<script>
  $("#login_form").validate({
  submitHandler: {
    function formhash(form, password) {
       // Create a new element input, this will be out hashed password field.
       var p = document.createElement("input");
       // Add the new element to our form.
       form.appendChild(p);
       p.name = "p";
       p.type = "hidden"
       p.value = hex_sha512(password.value);
       // Make sure the plaintext password doesn't get sent.
       password.value = "";
       // Finally submit the form.
       alert("Test");
       form.submit();
    }
  }
  });
</script>

2 个答案:

答案 0 :(得分:0)

您必须将loginform设置为表单标记的id属性。当您使用$('#loginform')作为选择器时。

答案 1 :(得分:0)

您的JS中存在语法错误。您不需要将formhash函数包装在大括号中。试试这个:

$("#login_form").validate({
    submitHandler: function formhash(form, password) {
        //submit code
    }
});