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

后端 未结 6 689
你的背包
你的背包 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:04

    Just place this in your JS for the page the submit button is on

    <script type="text/javascript">
      $(document).ready(function(){
        $("input[type='submit']").attr("disabled", false);
        $("form").submit(function(){
          $("input[type='submit']").attr("disabled", true).val("Please wait...");
          return true;
        })
      })
    </script>
    

    Code source

    0 讨论(0)
  • 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?

    0 讨论(0)
  • 2020-12-31 01:10

    Take any form of button and accrue its value to a variable. Replace the value with "please wait..." and after 5 seconds pass the previous saved variable back and disable to false.

        $('input[type=submit], input[type=button]').click(function(){
        var btn = $(this);
        var textd = btn.attr("value");
        btn.prop('disabled', true);
        btn.attr('value', 'Please wait...');
        setTimeout(function(){
        btn.prop('disabled', false);
        btn.attr('value', textd);
        }, fewSeconds*1000);
        });
    
    0 讨论(0)
  • 2020-12-31 01:10
            $('#btn').live('click', function () {
                $(this).prop('disabled', true).delay(800).prop('disabled', false);
            });
    

    //Or

     $('#btn').live('click', function () {
                var obj = $(this);
                obj.prop('disabled', true);
                $.post(base_url + 'folder/controller/function', $('#formID').serialize(), function (data) {
                    $('#message').slideDown().html('<span>' + data + '</span>');
                    obj.prop('disabled', false);
                });
            });
    
    0 讨论(0)
  • 2020-12-31 01:14

    In extension of @Teneff answer if there is multiple submit button in the form and you want the name of button at server side.

    <script type="text/javascript">
    var fewSeconds = 5;
    $('button').click(function(){
        var btn = $(this);
        var text = btn.text();
        setTimeout(function(){
            btn.prop('disabled', true);
            btn.text("Wait...");
        }, 10);
        setTimeout(function(){
            btn.prop('disabled', false);
            btn.text( text);
        }, fewSeconds*1000);
    });
    <script>
    
    0 讨论(0)
  • 2020-12-31 01:22

    You can do what you wanted like this:

    var fewSeconds = 5;
    $('#btn').click(function(){
        // Ajax request
        var btn = $(this);
        btn.prop('disabled', true);
        setTimeout(function(){
            btn.prop('disabled', false);
        }, fewSeconds*1000);
    });
    

    or you can use jQuery's .complete() method which is being executed after the ajax receives a response:

    $('#btn').click(function(){
        var btn = $(this);
        $.post(/*...*/).complete(function(){
            btn.prop('disabled', false);
        });
        btn.prop('disabled', true);
    
    });
    

    edit: this is an example with 3 seconds server-side response delay


    Edit

    Since even the answer is 8 years old, but still getting attention and jQuery is getting less popular I think it's worth adding example without jQuery

    const fewSeconds = 3
     
    document.querySelector('#btn').addEventListener('click', (e) => {
        e.target.disabled = true
      setTimeout(() => {
       e.target.disabled = false
      }, fewSeconds * 1000)
    })
    <input type='button' id="btn" value="click me" />

    0 讨论(0)
提交回复
热议问题