2

我正在使用 asp .net mvc 4,并且我有一个使用 ajax 验证的 html 表单。

我使用此 javascript 代码来了解表单是否无效。此代码有效。

$('#form').bind('invalid-form.validate', function () {
            alert("error");
            return false;
        });

但我找不到成功的 javascript 回调?

我试过了 :

$('#form').bind('valid-form.validate', function () {
            alert("success");

        });

或者

 $('#form').validate({
                invalidHandler:
                function (form, validator) {
                    alert('error');
                },
                rules: function (form, validator) {
                    alert('success ');
                }
            });

但我从来没有收到成功警报。谢谢你的帮助。

4

2 回答 2

1

使用 ajax.beginform

@using (Ajax.BeginForm("Delete",new{id = Model.id}, new AjaxOptions 
  {
   OnSuccess = "OnSuccess",
   OnFailure = "OnFailure",
   HttpMethod="Post"
 }))

然后将 OnSuccess/Onfailure 更改为您的 java 脚本函数的名称

于 2013-07-25T07:32:07.437 回答
0

像这样绑定到 form.submit -

$('#form').on('submit', function (e) {
    e.preventDefault();
        if ($(this).valid()) { // This checks if the form is valid
            alert("success"); // Or instead of success you can hide the form here
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                   // Goes in here after your MVC controller action is done
                },
                error: function (result) {
                   // Goes in here if your mvc controller action returns an error                   
                }
            });
        };
});
于 2013-07-25T15:29:57.607 回答