ASP.NET MVC - How to prevent double click submit with jquery.validate.unobtrusive lib?

后端 未结 9 1114
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 07:47

I need to avoid the double click submitting behavior. I\'m using the client validation with the unobtrusive library. I have the following code for avoiding the double clic:<

9条回答
  •  醉梦人生
    2020-12-29 08:08

    I have a form that uses MVC3 unobtrusive validation, and a viewmodel with a [RemoteAttribute]. It looks to me like the form's submit event only fires after all validation has passed. I'm currently using this, and it seems to work:

    
    
    $('form').live('submit', function() {
        $(this).find('input[type="submit"][data-app-disable-on-submit="true"]')
                    .attr('disabled', 'disabled');
    })
    

    ;

    I set breakpoints on both the remote attribute validation action method and the HttpPost action method. Clicking the submit button the first time hits the breakpoint on the validation action method. At this point, the button is still enabled. I can click it multiple times, and after resuming the validation method, the HttpPost is hit only once. When the HttpPost is hit, the submit button is disabled.

    Update

    Right you are Alex. So an updated version of the above would look like this:

    $('form').on('submit', function() {
        $(this).find('input[type="submit"][data-app-disable-on-submit="true"]')
                    .attr('disabled', 'disabled');
    })
    

提交回复
热议问题