问题描述
我正在使用ASP.NET MVC,并且在我的捆绑包中有jQuery,jQuery验证,jQuery validate.config,jQuery验证不引人注目的,不引人注意的Ajax和其他库.我的页面中有一个表单,并且其中包含一些字段.它有一个类型为button的按钮,当用户单击该按钮时,我试图做一些事情,然后做一个$(form).submit()
,我希望它被jQuery validate方法中的commithandler拦截,而不是提交它将对我的服务器进行ajax发布.我想使用jQuery.validate方法的原因是我在表单中有很多条件必填字段和其他验证.但是由于某种原因,jQuery验证完全不执行/不执行,并且表单正在提交而不是被提交处理程序拦截.我不知道为什么如果我正确地设置了一切,它为什么不起作用.这是我对jQuery validate方法的调用.我从来没有看到过警告说的很好.
I'm using ASP.NET MVC and I have jQuery, jQuery validate, jQuery validate.config, jQuery validate unobtrusive, unobtrusive ajax and other libraries in my bundle. I have a form in my page and has some fields inside of it. It has a button of type button and when the user clicks the button I'm trying to do some stuff and then do a $(form).submit()
, which I want it to be intercepted by the submithandler in the jQuery validate method and instead of submitting the form it will do a ajax post to my server. The reason I want to use the jQuery.validate method is that I have a lot of conditional required fields and other validations inside the form. But for some reason the jQuery validate is not executing/firing at all and the form is getting submitted instead of being intercepted by the submit handler. I have no idea why its not working if I setup everything correctly. Here is my call of the jQuery validate method. I never see the alert saying is good.
$('form[name="submitCheckoutForm"]').validate({
debug: true,
rules: {
"Password": {
required: true
}
},
submitHandler: function (form) {
//actions.SubmitCheckout();
alert('is good');
return false;
}
});
推荐答案
如果您使用的是Unobtrusive Validation插件,那么您也无法调用jQuery Validate的.validate()
方法.
If you're using the Unobtrusive Validation plugin, then you cannot also call jQuery Validate's .validate()
method.
为什么?
-
Unobtrusive Validation插件自动根据表单中的各种数据属性构造
.validate()
方法.
The Unobtrusive Validation plugin automatically constructs the
.validate()
method based on the various data attributes in your form.
jQuery Validate插件不允许在同一表单上多次调用.validate()
方法.因此,由于您已经在使用Unobtrusive Validation构造.validate()
,因此对.validate()
的其他调用将被完全忽略.
The jQuery Validate plugin does not allow the .validate()
method to be called more than once on the same form. So since you're already using Unobtrusive Validation to construct .validate()
, your other call to .validate()
will be totally ignored.
这篇关于使用非侵入式验证时,jQuery Validate未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!