Prevent double submission of forms in jQuery

后端 未结 22 1739
旧时难觅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条回答
  •  闹比i
    闹比i (楼主)
    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;
    };
    

提交回复
热议问题