使用 jQuery Unobtrusive Validation 时如何添加“submitHandler"函数? [英] How to add a 'submitHandler' function when using jQuery Unobtrusive Validation?
问题描述
我正在使用 ASP.NET MVC 3 中新的非侵入式验证功能验证表单.
I'm validating a form using the new unobtrusive validation features in ASP.NET MVC 3.
因此,我没有编写代码来设置 jQuery 验证以开始验证我的表单.这一切都是通过加载 jQuery.validate.unobtrusive.js 库来完成的.
So there is no code that I have written to setup jQuery validate to start validating my form. Its all done by loading the jQuery.validate.unobtrusive.js library.
不幸的是,我需要回答你确定吗?"表单有效但提交前的消息框.使用 jQuery 验证,您将在初始化时添加选项 handleSubmit,如下所示:
Unfortunately I need to whack in a 'are you sure?' message box when the form is valid but before submitting. With jQuery validate you would add the option handleSubmit when initialising like so:
$("#my_form").validate({
rules: {
field1: "required",
field1: {email: true },
field2: "required"
},
submitHandler: function(form) {
if(confirm('Are you sure?')) {
form.submit();
}
}
});
但是使用不显眼的库时不需要初始化.
But you don't need to initialise when using the unobtrusive library.
在这种情况下我可以在哪里/如何添加提交处理程序的任何想法?
Any ideas where/how I can add a submit handler in that case?
谢谢
推荐答案
不显眼的库会在所有表单上附加验证器,所以你必须这样替换submitHandler:
The unobtrusive library will attach the validator on all forms, so you have to replace the submitHandler this way:
$("form").data("validator").settings.submitHandler = function (form) { alert('submit'); form.submit(); };
这篇关于使用 jQuery Unobtrusive Validation 时如何添加“submitHandler"函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!