Prevent double submission of forms in jQuery

后端 未结 22 1340
旧时难觅i
旧时难觅i 2020-11-22 08:10

I have a form that takes a little while for the server to process. I need to ensure that the user waits and does not attempt to resubmit the form by clicking the button agai

相关标签:
22条回答
  • 2020-11-22 08:33

    My solution:

    // jQuery plugin to prevent double submission of forms
    $.fn.preventDoubleSubmission = function () {
        var $form = $(this);
    
        $form.find('[type="submit"]').click(function () {
            $(this).prop('disabled', true);
            $form.submit();
        });
    
        // Keep chainability
        return this;
    };
    
    0 讨论(0)
  • 2020-11-22 08:36

    I think Nathan Long's answer is the way to go. For me, I am using client-side validation, so I just added a condition that the form be valid.

    EDIT: If this is not added, the user will never be able to submit the form if the client-side validation encounters an error.

            // jQuery plugin to prevent double submission of forms
            jQuery.fn.preventDoubleSubmission = function () {
                $(this).on('submit', function (e) {
                    var $form = $(this);
    
                    if ($form.data('submitted') === true) {
                        // Previously submitted - don't submit again
                        alert('Form already submitted. Please wait.');
                        e.preventDefault();
                    } else {
                        // Mark it so that the next submit can be ignored
                        // ADDED requirement that form be valid
                        if($form.valid()) {
                            $form.data('submitted', true);
                        }
                    }
                });
    
                // Keep chainability
                return this;
            };
    
    0 讨论(0)
  • 2020-11-22 08:36

    Please, check out jquery-safeform plugin.

    Usage example:

    $('.safeform').safeform({
        timeout: 5000,  // disable next submission for 5 sec
        submit: function() {
            // You can put validation and ajax stuff here...
    
            // When done no need to wait for timeout, re-enable the form ASAP
            $(this).safeform('complete');
            return false;
        }
    });
    
    0 讨论(0)
  • 2020-11-22 08:36

    I've been having similar issues and my solution(s) are as follows.

    If you don't have any client side validation then you can simply use the jquery one() method as documented here.

    http://api.jquery.com/one/

    This disables the handler after its been invoked.

    $("#mysavebuttonid").on("click", function () {
      $('form').submit();
    });
    

    If you're doing client side validation as I was doing then its slightly more tricky. The above example would not let you submit again after failed validation. Try this approach instead

    $("#mysavebuttonid").on("click", function (event) {
      $('form').submit();
      if (boolFormPassedClientSideValidation) {
            //form has passed client side validation and is going to be saved
            //now disable this button from future presses
            $(this).off(event);
       }
    });
    
    0 讨论(0)
  • 2020-11-22 08:38

    Use simple counter on submit.

        var submitCounter = 0;
        function monitor() {
            submitCounter++;
            if (submitCounter < 2) {
                console.log('Submitted. Attempt: ' + submitCounter);
                return true;
            }
            console.log('Not Submitted. Attempt: ' + submitCounter);
            return false;
        }
    

    And call monitor() function on submit the form.

        <form action="/someAction.go" onsubmit="return monitor();" method="POST">
            ....
            <input type="submit" value="Save Data">
        </form>
    
    0 讨论(0)
  • 2020-11-22 08:38

    I can't believe the good old fashioned css trick of pointer-events: none hasn't been mentioned yet. I had the same issue by adding a disabled attribute but this doesn't post back. Try the below and replace #SubmitButton with the ID of your submit button.

    $(document).on('click', '#SubmitButton', function () {
        $(this).css('pointer-events', 'none');
    })
    
    0 讨论(0)
提交回复
热议问题