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

后端 未结 9 1113
伪装坚强ぢ
伪装坚强ぢ 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:13

    I was able to fix a similar issue with a couple of lines of code. I prefer this if you don't want to "alert" to user that they double clicked and just silently ignore the second click.

    I just made a global javascript variable that I toggled when my function was executing during a critical section. This kept subsequent function calls from re-executing the same section.

     var criticalSection = false;
    
     SomeOnClickEventFired = function () {
          if (!criticalSection)
          {
                criticalSection = true;
                //Ajax Time
                criticalSection = false;
          }
     }
    
    0 讨论(0)
  • 2020-12-29 08:18

    Extends answers by Alex and Ryan P to accounts for situations where jQuery Validation might be missing and where multiple submit buttons exist in a single form.

    oneClickSubmitButton = function () {
        $('input[type=submit], button[type=submit], input[type=image]').each(function () {
            var $theButton = $(this);
            var $theForm = $theButton.closest('form');
    
            //hide the button and submit the form
            function tieButtonToForm() {
                $theButton.one('click', function () {
                    $theButton.addClass('ui-state-disabled');
                });
            }
    
            tieButtonToForm();
    
            $theForm.submit(function (event) {
                // Only proceed for the clicked button
                if (!$theButton.hasClass("ui-state-disabled"))
                    return;
    
                // If jQuery Validation is not present or the form is valid, the form is valid
                if (!$theForm.valid || $theForm.valid())
                    return;
    
                // Re-wire the event
                $theButton.removeClass('ui-state-disabled');
                event.preventDefault();
                tieButtonToForm();
            });
        });
    };
    
    0 讨论(0)
  • 2020-12-29 08:22
     $('form').submit(function () {
     $('input[type="submit"]', this).attr('disabled', 'disabled');
       });
    
    0 讨论(0)
提交回复
热议问题