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

时间:2022-11-24 10:56:30

On submit button click I need to check the validation if its returns true. I need to submit the form. But when it returns false also the form gets submitted and the event SaveProjects() gets called. How can I prevent the form submission when CheckValidation() returns false?

在提交按钮单击我需要检查验证,如果它返回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 个解决方案

#1


1  

you can do

你可以做

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

#1


1  

you can do

你可以做

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