问题描述
我有一个与我的提交按钮相关联的 jQuery 函数,如下所示:
I have a jQuery function tied to my submit button like this:
$(function () {
$('#signupform').submit(function () {
alert('test');
});
});
但是,无论表单是否有效,它都会触发.我的模型装饰有各种 DataAnnotations 并且客户端验证运行良好,但我只希望 jQuery 函数在表单经过验证时触发.我该如何做到这一点?
However, it fires whether or not the form is valid. My model is decorated with various DataAnnotations and the client-side validation is working well, but I only want that jQuery function to fire if the form has been validated. How do I accomplish that?
为了澄清,我使用 MVC DataAnnotations + jQuery 的不显眼的 javascript 来处理客户端验证.我没有编写自己的 javascript 验证例程.内置的 jQuery 验证在验证表单方面做得很好,但我只需要知道如何在我自己的函数中将该验证的结果转换为布尔值.
To clarify, I'm using MVC DataAnnotations + jQuery's unobtrusive javascript to handle the client-side validation. I do not have my own javascript validation routines written. The built in jQuery validation is doing a great job of validating the form, but I just need to know how to get the results of that validation into a boolean value within my own function.
推荐答案
如果你使用 jquery validate 不显眼的验证,你可以:
If you are using jquery validate unobtrusive validation you could:
$(function () {
$('#signupform').submit(function () {
if($(this).valid()) {
alert('the form is valid');
}
});
});
这篇关于仅当表单有效时如何触发 jQuery 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!