jQuery验证-使用函数重置表单,resetForm()将不起作用 [英] JQuery Validate - reset form using a function, resetForm() won't work
问题描述
我正在使用JQuery Validator,并且在stackoverflow中看到了许多解决方案,将所有验证都放在"var"中,然后使用此"var" .resetForm()重置对象,但这在这里不起作用是我的示例的 JSFiddle .
I'm using JQuery Validator and I saw many solutions here in stackoverflow, putting all validation in a "var" and then using this "var" .resetForm() to reset the object, but this isn't working, here is my JSFiddle with my example.
稍后我的意图:使用相同的表格,内部使用两种类型的块,且输入不同,并且当其中一个块发生更改时,我调用一种方法来重置验证,以便在尝试时可以再次使用Validate提交.
My intention later: use same Form, two types of blocks with differents inputs inside and when one of blocks change, I call a method to reset the validation so I can reuse the Validate again when try to submit.
var validate = $('#cad_principal').validate({
errorLabelContainer: $('.form_validate_message'),
wrapper:'li',
rules:{
cad_pf_cpf:{required:true, minlength:11}
},
messages:{
cad_pf_cpf:{required:"O campo CPF precisa ser preenchido.", minlength:"O campo CPF deve conter no mínimo 11 caracteres."}
},
submitHandler: function(form) {
$(form).submit();
}
});
function cancelForm(){
validate.resetForm();
}
推荐答案
您要调用的函数(cancelForm()
)不存在.试试这个:
The function you're trying to call (cancelForm()
) doesn't exist. Try this:
$(function() {
var validate = $('#cad_principal').validate({
errorLabelContainer: $('.form_validate_message'),
wrapper:'li',
rules:{
cad_pf_cpf:{required:true, minlength:11}
},
messages:{
cad_pf_cpf:{required:"O campo CPF precisa ser preenchido.", minlength:"O campo CPF deve conter no mínimo 11 caracteres."}
},
submitHandler: function(form) {
$(form).submit();
}
});
$('#cad_principal a').click(function() { validate.resetForm(); });
});
基本上,您需要将代码包装在DOMReady事件中,然后使用事件绑定来连接表单.请记住,validate
仅在DOM准备就绪时将包含一个变量.这是一个 正在运行的jsFiddle .
Basically, you need to wrap the code inside the DOMReady event, and then use event binding to hook up the form. Remember that validate
will only contain a variable when the DOM is ready. Here's a working jsFiddle.
这篇关于jQuery验证-使用函数重置表单,resetForm()将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!