2012-11-27 57 views
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吗? 有没有更好的方法来做到这一点?

在此先感谢。

回答

1

尝试使用一个变量(save)而不是多个return语句:

$('#form_section11').submit(function (e) { 
    'use strict'; 
    var self = $(this), 
     save = false, 
     FamilyNameExists = true, 
     DateOfBirthExists = true, 
     validatorSection11 = self.validate(); 
    if (!self.valid()) { 
     // or family name and date of birth 
     FamilyNameExists = true; 
     DateOfBirthExists = true; 
     if (validatorSection11.errorMap["FamilyName"]) { 
      FamilyNameExists = false; 
     } 
     if (validatorSection11.errorMap["DateOfBirth"]) { 
      DateOfBirthExists = false; 
     } 
     // show the partial save rules 
     $('#ParitalSaveIntructions').show(); // should this be "Parital" or "Partial" 
     // show the partial save checkbox 
     if (FamilyNameExists && DateOfBirthExists) { 
      $("#AgreePartialSave").show(); 
      //if ($("#PartialSave").is(':checked')) { // Comment in answer 
      // // partial save has been requested 
      // save = true; 
      //} 
      save = $("#PartialSave").is(':checked'); 
      // clear perinatalWomanView_PartialSave 
      $("#PartialSave").removeAttr('checked'); 
     } 
     //save = false; //This was overriding the `save = true` above. 
    } 
    if (!save) { 
     e.preventDefault(); // stops form from submitting immediately 
    } 
    return save; 
}); 

此外,在部分“在回答评论”,这一部分将有可能只执行形式已重新提交后,因为以下将不得不发生:

  1. $("#AgreePartialSave").show();必须执行并显示该部分。
  2. 用户必须为$("#PartialSave").is(':checked')支票$("#PartialSave")返回true
  3. $('#form_section11').submit()必须再次为处理程序的该部分进行评估。

如果有一个不同的按钮,用户必须单击才能执行部分保存,则可能需要将整个部分移动到该按钮处理程序中。

+0

嗨皮特,感谢以上。恐怕它没有什么区别,所以问题可能在别处。我怀疑,不管是按照您的建议,还是使用忽略选项都可能是未来的方向。 –

+0

@PeterSmith:我发现了一个逻辑问题。更新了答案。请尝试一下,让我知道它是否工作。 – pete