0
我正在使用MVC3和unobstrusive验证。在某些情况下,即使表格不完整,我也想提交表格,只要选择了两个字段。我正在使用的JavaScript如下:当表单不完整时提交jQuery验证表单
$('#form_section11').submit(function() {
//event.preventDefault(); //stops form from submitting immediately
var validatorSection11 = $('#form_section11').validate();
if (!$(this).valid()) {
// or family name and date of birth
var FamilyNameExists = true;
var DateOfBirthExists = true;
if (validatorSection11.errorMap["FamilyName"]) { FamilyNameExists = false; }
if (validatorSection11.errorMap["DateOfBirth"]) { DateOfBirthExists = false; }
// show the partial save rules
$('#ParitalSaveIntructions').show();
// show the partial save checkbox
if (FamilyNameExists && DateOfBirthExists) {
$("#AgreePartialSave").show();
if ($("#PartialSave").is(':checked')) {
// partial save has been requested
return true; //// <- save not happening, INCORRECT action
}
// clear perinatalWomanView_PartialSave
$("#PartialSave").removeAttr('checked');
}
return false; // <- save not happening, correct action
}
return true; // <- save happens, correct action
});
用户被提供一个复选框以确认不完整的提交。我已经指出了JavaScript的工作原理和失败的地方。
我还添加了
var validatorSection11 = $('#form_section11').validate(
{
onsubmit: false
}
);
,没有任何效果。我的问题是:
为什么原始return true
无法正常工作? 我正确使用onsubmit: false
吗? 有没有更好的方法来做到这一点?
在此先感谢。
嗨皮特,感谢以上。恐怕它没有什么区别,所以问题可能在别处。我怀疑,不管是按照您的建议,还是使用忽略选项都可能是未来的方向。 –
@PeterSmith:我发现了一个逻辑问题。更新了答案。请尝试一下,让我知道它是否工作。 – pete