如何挂钩到jQuery的误差在MVC 3验证不显眼? [英] How to hook into error of jQuery validate unobtrusive in MVC 3?

查看:181
本文介绍了如何挂钩到jQuery的误差在MVC 3验证不显眼?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找一种方式挂接到客户端失败条件的形式。

我需要重新启用如果验证失败,使他们可以再次尝试一个提交按钮。

我发现了以下内容:<一href=\"http://stackoverflow.com/questions/5226806/using-unobtrusive-validation-in-asp-net-mvc-3-how-can-i-take-action-when-a-form\">Using不显眼的验证在ASP.NET MVC 3,我怎么能采取行动时,一种形式是无效的?这可能会工作,但我希望从一个集中的地方这样做的表格可以从好几个地方被提交。

更新:

这个功能似乎运作良好假设你有一个类.goButton输入标签。

 &LT; SCRIPT LANGUAGE =JavaScript的类型=文/ JavaScript的&GT;
    $(。goButton)。点击(函数(){
        如果(!$(本).closest(形式)。有效的()){
            返回false;
        }
        $(goButton)后(请稍候...)。
        $(goButton)隐藏()。
    });
&LT; / SCRIPT&GT;


解决方案

然后就可以钩从中央地方所有形式的 - 只需要知道所有形式的会大呼过瘾。相反,样品中使用 $(#formId),只需使用 $(形式)。提交()和代表将被要求对任何形式的提交,并在该方法中,你可以打电话给你的验证检查,并返回true(提交表单),或假以prevent它。

像这样把我的头顶部

  $(形式)。递交(函数(){
  如果(!$(本).valid()){
      返回false;
  }
  其他
  {
      返回true;
  }
});

Looking for a way to hook into the client side fail condition for the form.

I need to re-enable a submit button if the validation fails so they can try again.

I found the following: Using unobtrusive validation in ASP.NET MVC 3, how can I take action when a form is invalid? which might work but I was hoping to do this from a central place as the form can be submitted from several places.

Update:

This function seems to work well assuming you have an input tag with the class ".goButton".

<script language="javascript" type="text/javascript">
    $(".goButton").click(function () {
        if (!$(this).closest("form").valid()) {
            return false;
        }
        $(".goButton").after("Please wait...");
        $(".goButton").hide();
    });
</script>

解决方案

Then you can hook ALL forms from a central place - just be aware all forms will be hooked. Instead of using $("#formId") in the sample, simply use $("form").submit() and that delegate will be called for any form's submit and in that method you can call your validate check and return true (to submit the form) or false to prevent it.

Something like this off the top of my head

$("form").submit(function () {
  if (!$(this).valid()) {
      return false;
  }
  else
  {
      return true;
  }
});

这篇关于如何挂钩到jQuery的误差在MVC 3验证不显眼?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆