Simplest way to disable button on submission of a form?

前端 未结 5 1993
执念已碎
执念已碎 2020-12-15 04:10

I\'ve been trying to find the \"right\" way to prevent double submits of forms. There are lots of related posts on SO but none of them hit the spot for me. Two questions b

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 04:53

    Similarly Ive seen a few examples, this one allows you to alter how long the button is disabled for, through timeout. It also only triggers on a form submit rather than on the buttons click event, which originally caused me a few issues.

        $('form').submit(function () {
            var button = $('#button');
            var oldValue = button.value;
            var isDisabled = true;
    
            button.attr('disabled', isDisabled);
    
            setTimeout(function () {
                button.value = oldValue;
                button.attr('disabled', !isDisabled);
            }, 3000)
        });
    

提交回复
热议问题