我同时使用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>
&#13;
请帮忙吗?
答案 0 :(得分: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();