javascript 手动触发 jQuery 验证?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6404671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trigger jQuery Validation Manually?
提问by Mikkel
I have i form i submit via jQuery's .submit(), unfortunately it does not trigger the validation plugin when i do it this way. If i place a submit button inside the form, it works perfectly.
我有我通过 jQuery 的 .submit() 提交的表单,不幸的是,当我这样做时它不会触发验证插件。如果我在表单中放置一个提交按钮,它就可以完美运行。
Is there someway to trigger the plugin manually?
有没有办法手动触发插件?
Thanks in advance.
提前致谢。
回答by Shef
$("#myButton").click(function(e) {
e.preventDefault();
if($("#myform").valid()){
// the form is valid, do something
} else{
// the form is invalid
}
});
回答by Darin Dimitrov
You could try this:
你可以试试这个:
$('#someForm').valid();
which could also be done in the submit handler:
这也可以在提交处理程序中完成:
$('#someForm').submit(function() {
if ($(this).valid()) {
} else {
}
});
回答by Pantelis
You can use this
你可以用这个
$(".selector").validate({
submitHandler: function(form){
form.submit();
}
});
which autosubmits the form it is valid, so you .validate() instead of submit()
它自动提交表单它是有效的,所以你 .validate() 而不是 submit()