jquery auto提交自己

时间:2016-12-15 15:21:54

标签: javascript jquery html ajax

任何人都明白为什么这个汽车提交表格?似乎找不到正确答案的原因。

使用查询parsley以模态验证表单。

用户打开模式,用户开始在文本区域输入,您必须至少输入20个字符并限制为100.当您超过20时,表单会自行提交。

无论我做什么,我都无法防止这种情况发生。

有任何线索吗?

谢谢=)

                  <div id="form-content" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-hidden="true">
                <div class="modal-dialog modal-sm">
                  <div class="modal-content">

                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">X</span>
                      </button>
                      <h4 class="modal-title" id="myModalLabel2">Modal title</h4>
                    </div>
                    <div class="modal-body">
                <!-- start form for validation -->
                <form id="ReportForm">

                  <label for="fullname">Brukernavn :</label>
                  <input type="hidden" id="Username_Field" class="form-control" name="username" value="Kimmeliten" />

                      <label for="message">Message (20 chars min, 500 max) :</label>
                      <textarea id="message" required="required" class="form-control" name="message" data-parsley-trigger="keyup" data-parsley-minlength="20" data-parsley-maxlength="100" data-parsley-minlength-message="Come on! You need to enter at least a 20 caracters long comment.." data-parsley-validation-threshold="10"></textarea>


                </form>
                <!-- end form for validations -->
                                </div>
                    <div class="modal-footer">
                      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                      <button type="button" class="btn btn-primary" id="submit10">Save changes</button>
                    </div>
                  </div>
                </div>
              </div>
              <!-- /modals -->

    <script>
  $(document).ready(function() {
    $.listen('parsley:field:validate', function() {
      validateFront();
    });
    $('#submit10').click(function(){
      $('#ReportForm').parsley().validate();
      validateFront();
    });
    var validateFront = function() {
      if (true === $('#ReportForm').parsley().isValid()) {
        $('.bs-callout-info').removeClass('hidden');
        $('.bs-callout-warning').addClass('hidden');
                  $.ajax({
                    type: "POST",
                        url: "forum/ajax/report.ajax.php",
                        data: $('#ReportForm').serialize(),
                success: function(msg){
                            $("#thanks").html(msg);
                        $("#form-content").modal('hide');   
                        },
                                error: function (xhr, ajaxOptions, thrownError) {
                                        alert(xhr.status);
                                        alert(thrownError);
                                      }
                });            

      } else {
        $('.bs-callout-info').addClass('hidden');
        $('.bs-callout-warning').removeClass('hidden');
      }
    };
  });
  try {
    hljs.initHighlightingOnLoad();
  } catch (err) {}
</script> 

1 个答案:

答案 0 :(得分:1)

不确定,但查看documentation,显示:

  

form:validate |在验证之前触发表单验证时触发。

看来,一旦你开始验证你的监听器会被触发,触发validateFront的是什么。

$.listen('parsley:field:validate', function() {
  validateFront();
});

您是否尝试过聆听form:success