Using jQuery validation, is there a way I can reset a form without having a reference to the created object?

拜拜、爱过 提交于 2019-12-24 10:59:26

问题


I have a generic jQuery validation call in a global JS file:

$('form').each(function() {
    $(this).validate();
});

This way, any forms on the website can enable validation by simply providing metadata. However, now I want to reset the form because I'm doing AJAX submits. On the official website, it says to reset forms by using the created object:

var validator = $('#someForm').validate();

Problem is, with my generic validator setup, I don't have a reference to the created object. Is there a way I can still retrieve it in my case?


回答1:


I'm not sure I understand what you mean by reset the forms. Just in case...

You can remove validation like this..

$("form").removeData("validator");
$("form").removeData("unobtrusiveValidation");



回答2:


You can do like this ,

function ResetValidations(){
           $("form").removeData("validator");
           $("form").removeData("unobtrusiveValidation");
           $.validator.unobtrusive.parse("form");
    }



回答3:


var formValidations = [];
$('form').each(function() {
    formValidations.push($(this).validate());
});

Then iterate through formValidations

for(var i in formValidations)
{
    //do something with formValidations[i]
}


来源:https://stackoverflow.com/questions/7816191/using-jquery-validation-is-there-a-way-i-can-reset-a-form-without-having-a-refe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!