问题描述
我有一个html表单.我正在使用jquery.validate.js验证表单,并且可以在Submit事件上使用.我想在提交被解雇之前验证此表单.
I have a html form. i am validating my form using jquery.validate.js and it works on submit event. I want to validate this form before submit get fired.
根据我的R& D,我已经尝试过:
As per my R&D i have tried :
$('#my-form').on('before-submit',function(){
alert('Before submit performed');
});
我想在触发提交事件之前验证表单.谁能告诉我该怎么做?
I want to validate form Just before firing submit event.Can any one tell me how to do this?
非常感谢,
M
推荐答案
您可以模仿默认的jQuery插件行为,如下所示:
You can mimic the default jQuery plugin behaviour as follows:
测试form
是否有效;如果不能防止使用preventDefault()
进行默认操作:
Test if the form
is valid; if not prevent the default action from occurring using preventDefault()
:
$(document)
.on('click', 'form button[type=submit]', function(e) {
var isValid = $(e.target).parents('form').isValid();
if(!isValid) {
e.preventDefault(); //prevent the default action
}
});
编辑:如果您想微调验证测试(即有条件地验证某些控件),这将非常有用.
EDIT: this comes in handy if you want to fine-tune your validation test i.e. validating some controls conditionally.
这篇关于提交jquery之前验证表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!