0

我有一个表单,即使验证失败,它仍会提交给控制器。

jQuery:

$(document).ready(function () {
       $('#commentForm').validate();
});

 $(function () {
     $('#commentForm').submit(function () {
         $('#result').empty();
         $.ajax({
             url: "Action/Index",
             type: "POST",
             data: {
                 name: $('#name').val(),
                 emailAddress: $('#emailAddress').val()
             },
             success: function (result) {
              alert(result);

                 }
             }
         });
       return false;
     });
 });

html:

<form class="cmxform" id="commentForm">
<fieldset>
<div id="contactUsName">Your Name<br />
    <input type="text" id="name" class="required" />
</div>
<div id="contactUsEmail">Email Address<br />
    <input type="text" id="emailAddress" class="required email" />
</div>
</fieldset>
</form>
4

2 回答 2

3

您应该使用验证 submitHandler 属性,如下所示:

$('#commentForm').validate(
  submitHandler: function () {
         $('#result').empty();
         $.ajax({
             url: "Action/Index",
             type: "POST",
             data: {
                 name: $('#name').val(),
                 emailAddress: $('#emailAddress').val()
             },
             success: function (result) {
              alert(result);

             }
           }
         );
       return false;
     });
);
于 2012-08-28T15:26:19.327 回答
1

你有一个额外的括号}

             }
         } //<-- you should remove this one
     });

   return false;
于 2012-08-28T15:26:31.340 回答