表单永远不会使用jQuery验证插件提交 - 即使有效

时间:2013-01-26 13:40:27

标签: jquery jquery-validate

我正在使用jQuery Validation插件来验证我的表单,但即使成功验证后表单也不会提交。以下是重现问题的代码:

<?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>

请注意,在表单标记中有或没有有效操作时会发生这种情况。

导致这种情况的原因是什么?

1 个答案:

答案 0 :(得分:4)

debug设为false:

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

如果debug为真,则表单未提交:http://docs.jquery.com/Plugins/Validation/validate#options 例如。 http://codepad.viper-7.com/H4OFP5