How to hook into error of jQuery validate unobtrusive in MVC 3?

走远了吗. 提交于 2019-12-18 12:58:05

问题


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>

回答1:


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;
  }
});


来源:https://stackoverflow.com/questions/6494008/how-to-hook-into-error-of-jquery-validate-unobtrusive-in-mvc-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!