在提交按钮单击时验证失败时阻止表单提交

时间:2013-05-02 15:13:43

标签: javascript asp.net-mvc

在提交按钮上单击我需要检查验证是否返回true。我需要提交表格。但是当它返回false时,表单也会被提交并调用事件SaveProjects()。如何在CheckValidation()返回false时阻止表单提交?

 <script type="text/javascript> 
   function CheckValidation()
      {
       alert();
      return false;
      }

</script>
    @using (@Ajax.BeginForm("SaveProjects", "Projects", null, new AjaxOptions { OnSuccess =     "OnSuccessProject", HttpMethod = "post" }, new { id = "ProjectsForm" }))
       {     <div class="outerBox" id="divProjectCreate">

      @Html.Partial("../Projects/_CreateOrEdit", Model)
         <div class="row centerAlign">
          <input type="submit" value="Save" id="btnSaveProjects"  onclick="CheckValidation()"/>
         <input type="button" value="Cancel" id="btnCancel" />
         </div>

        </div>

}

1 个答案:

答案 0 :(得分:1)

你可以做到

   $('#target').submit(function() {
      alert('Handler for .submit() called.');
      return CheckValidation();
    });