0

我同时使用DirtyForms和 popupar jQuery Validate来验证我的表单。我还需要使用该submitHandler()方法,因为在实际发送表单之前我会执行一些操作。在这种情况下,即使在正常的提交表单上也会触发 DirtyForms,而这是不应该的。

$('.validate').validate({
	// submitHandler causer DirtyForms to be triggered evn on normal form submit
  submitHandler: function (form) {
    // do a couple of things here, then...
    form.submit();
  }
});

$('form[method="post"]').dirtyForms();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.dirtyforms/2.0.0/jquery.dirtyforms.min.js"></script>

<p>Fill the field and send the form, you'll see that DirtyForms is triggered even if it should not</p>
<form action="#" method="post" class="validate">
  <input type="text" name="username" required>
  <button type="submit">send</button>
</form>

请问,有什么帮助吗?

4

1 回答 1

1

由于submitHandler仅在表单有效时触发,因此您永远不需要 DirtyForms 在这里做任何事情。

所以只需在submitHandler使用中杀死它.dirtyForms('setClean')

$('.validate').validate({
    submitHandler: function(form) {
        // do a couple of things here, then...
        $('form:dirty').dirtyForms('setClean');  // <- kill DirtyForms
        form.submit();
    }
});

$('form[method="post"]').dirtyForms();

演示: jsfiddle.net/aunpmfsa/1/

于 2018-05-31T15:44:16.573 回答