【问题标题】:asp .net validation with jquery success callback带有 jquery 成功回调的 asp .net 验证
【发布时间】:2013-07-24 17:33:18
【问题描述】:

我正在使用 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 ');
                }
            });

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

【问题讨论】:

  • 您是在做 Ajax 表单帖子,还是常规帖子?您希望弹出窗口出现在从服务器返回之前还是之后?
  • 实际上我不想使用警报功能,现在它只是用于调试,我想通过执行类似 $("myid").hide() 的操作来隐藏 div。但我需要知道它是否成功

标签: jquery asp.net-mvc forms validation


【解决方案1】:

使用 ajax.beginform

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

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

【讨论】:

    【解决方案2】:

    像这样绑定到 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                   
                    }
                });
            };
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 2019-06-23
      • 2012-01-02
      • 2012-02-15
      相关资源
      最近更新 更多