问题
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