[英]DirtyForms does not work properly when used with submitHandler() method of jQuery Validate
I'm using both DirtyForms and the popupar jQuery Validate to validate my forms. 我同时使用DirtyForms和弹出式jQuery Validate来验证我的表单。 I also need to use the submitHandler()
method because I do a couple of actions before actually sending the form. 我还需要使用submitHandler()
方法,因为在实际发送表单之前,我做了一些操作。 In this scenario, DirtyForms is triggered even on a normal submit form, when it shouldn't be. 在这种情况下,即使不应该在常规提交表单上触发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>
Please, any help? 请帮忙吗?
Since submitHandler
only fires when the form is valid, you will never need DirtyForms to do anything here. 由于submitHandler
仅在表单有效时才触发,因此您将不需要DirtyForms在此处执行任何操作。
So just kill it within submitHandler
using .dirtyForms('setClean')
因此,只需使用.dirtyForms('setClean')
在submitHandler
杀死它
$('.validate').validate({
submitHandler: function(form) {
// do a couple of things here, then...
$('form:dirty').dirtyForms('setClean'); // <- kill DirtyForms
form.submit();
}
});
$('form[method="post"]').dirtyForms();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.