Form never submits using jQuery validation plugin - even when valid

老子叫甜甜 提交于 2019-12-19 09:44:57

问题


I am using a jQuery Validation plugin to validate my form, but the form does not submit even after successful validation. Here is code to reproduce the issue:

<?php
if(isset($_REQUEST['submit'])){
    echo "form submitted";  //have to insert into database here
}
?>

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<script type="text/javascript">
jQuery.validator.setDefaults({
    debug: true,
    success: "valid"
});;
</script>
<script>
  $(document).ready(function(){
    $("#myform").validate({
  rules: {
    field1: {
      required: true,
      number: true
    },
    field2: {
      required: true,
      number: true,
      minlength: 3
    },
    field3: {
      required: true,
      email: true
    }
  }
});
  });
  </script>


    <form id="myform" method="post" action="">
      <div>
        <label for="field">Required, decimal number: </label>
        <input class="left" id="field1" name="field1" type="text" />
      </div>
      <br>
      <div>
        <label for="field">Required, Minimum length 3: </label>
        <input class="left" id="field2" name="field2" type="text" />
      </div>
      <br>
      <div>
        <label for="field">Required, email: </label>
        <input class="left" id="field3" name="field3" type="text" />
      </div>
      <br/>
      <input type="submit" name="submit" value="Validate!" />
    </form>

Note, this happens with or without a valid action in the form tag.

What could be causing this?


回答1:


Set debug to false:

<script type="text/javascript">
jQuery.validator.setDefaults({
    debug: false,
    success: "valid"
});;
</script>

If debug is true, the form is not submitted: http://docs.jquery.com/Plugins/Validation/validate#options E.g. http://codepad.viper-7.com/H4OFP5



来源:https://stackoverflow.com/questions/14537476/form-never-submits-using-jquery-validation-plugin-even-when-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!