使用Submit()函数时不会触发不打扰的验证 [英] unobtrusive validation not triggering when using submit() function
问题描述
在我看来,我有一个提交表单的按钮:
In my view I have a button that submits the form:
<输入类型=按钮" value =仅保存" id =保存" onclick ="submitForm('SaveOnly')"/>
这是几个不同的按钮之一,所有这些按钮将在不同的Controller Action方法中进行不同的处理.
This is one of a few different buttons, all of which will be handled differently in different Controller Action methods.
在视图中也是此 submitForm(action)
Javascript方法:
Also in the view is this submitForm(action)
Javascript method:
function submitForm(action) {
var form = document.getElementById("myForm");
form.action = "/Area/MyController/" + action;
form.submit();
}
当我使用 submit
按钮(而不是 type ="button"
)时,不打扰的客户端验证可以正常工作.但是,当我使用此 submitForm(action)
函数时,不会进行客户端验证.如何触发客户端验证?
When I use a submit
button (rather than type="button"
), unobtrusive client-side validation works properly. However, when I use this submitForm(action)
function, the client-side validation does not happen. How can trigger the client side validation?
推荐答案
您可以按以下方式手动调用它:
You can call it manually as follows:
function submitForm(action) {
var $form = $("#myForm");
$form.action = "/Area/MyController/" + action;
$.validator.unobtrusive.parse($form);
$form.validate();
if ($form.valid()) {
$form.submit();
}
}
这篇关于使用Submit()函数时不会触发不打扰的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!