调用JQuery验证插件而不提交表单
问题描述:
是否可以在不使用JQuery验证插件提交的情况下验证表单?
Is it possible to validate the form without submitting it using the JQuery validation plugin?
我使用button.click函数进行验证,该按钮不是提交输入.
I use a button.click function for validating, the button isn't a submit input.
答
是的,可以在不提交表单的情况下测试其有效性.
Yes, it is possible to test the form's validity without submitting it.
每当调用.valid()
时,都会对表单进行测试,该方法还将返回一个布尔值.
Whenever .valid()
is called the form is tested, and the method will also return a boolean.
$(document).ready(function () {
$('#myform').validate({ // initialize the plugin
// rules & options
});
$('#button').click(function() {
if ($('#myform').valid()) {
alert('form is valid - not submitted');
} else {
alert('form is not valid');
}
});
});
工作演示: http://jsfiddle.net/zMYVq/
Working DEMO: http://jsfiddle.net/zMYVq/