如何使用jQuery不显眼的验证时添加一个'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的验证开始验证我没有形式code。所有通过加载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?
THX
推荐答案
不显眼的图书馆将附上所有形式的验证,所以你必须更换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不显眼的验证时添加一个'submitHandler“功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!