Disable the submit button after clicking and enable it back again after a few seconds

后端 未结 6 690
你的背包
你的背包 2020-12-31 00:47

I\'m using jquery $.post when submitting a form. I want to disable the button for like 5 seconds after it has been clicked to avoid multiple submission of the form.

6条回答
  •  感动是毒
    2020-12-31 01:05

    Have a look at the .success function here it's what you need.

    so what you do is disable button on click

    $('#btn').click(function(){
        $('#btn').attr('disabled', true);
        $.post(base_url + 'folder/controller/function', $('#formID').serialize(), function(data) {
            // code on completion here
            })
            .success(function() { 
               $('#btn').attr('disabled', false);
            })
        });
    });
    

    This way is better as what happens if your request takes more than 5 seconds to return?

提交回复
热议问题