Prevent double submission of forms in jQuery

后端 未结 22 1322
旧时难觅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:17

    Timing approach is wrong - how do you know how long the action will take on client's browser?

    How to do it

    $('form').submit(function(){
      $(this).find(':submit').attr('disabled','disabled');
    });
    

    When form is submitted it will disable all submit buttons inside.

    Remember, in Firefox when you disable a button this state will be remembered when you go back in history. To prevent that you have to enable buttons on page load, for example.

    0 讨论(0)
  • 2020-11-22 08:19

    Why not just this -- this will submit the form but also disable the submitting button,

       $('#myForm').on('submit', function(e) {
           var clickedSubmit = $(this).find('input[type=submit]:focus');
           $(clickedSubmit).prop('disabled', true);
       });
    

    Also, if you're using jQuery Validate, you can put these two lines under if ($('#myForm').valid()).

    0 讨论(0)
  • 2020-11-22 08:21

    ...but the form is not submitted with any of the POST data it is supposed to include.

    Correct. Disabled form element names/values will not be sent to the server. You should set them as readonly elements.

    Also, anchors cannot be disabled like that. You will need to either remove their HREFs (not recommended) or prevent their default behaviour (better way), e.g.:

    <script type="text/javascript">
    $(document).ready(function(){
        $("form#my_form").submit(function(){
          $('input').attr('readonly', true);
          $('input[type=submit]').attr("disabled", "disabled");
          $('a').unbind("click").click(function(e) {
              e.preventDefault();
              // or return false;
          });
        });
    </script>
    
    0 讨论(0)
  • 2020-11-22 08:22

    Use two submit buttons.

    <input id="sub" name="sub" type="submit" value="OK, Save">
    <input id="sub2" name="sub2" type="submit" value="Hidden Submit" style="display:none">
    

    And jQuery:

    $("#sub").click(function(){
      $(this).val("Please wait..");
      $(this).attr("disabled","disabled");
      $("#sub2").click();
    });
    
    0 讨论(0)
  • 2020-11-22 08:23

    I ended up using ideas from this post to come up with a solution that is pretty similar to AtZako's version.

     jQuery.fn.preventDoubleSubmission = function() {
    
        var last_clicked, time_since_clicked;
    
        $(this).bind('submit', function(event){
    
        if(last_clicked) 
          time_since_clicked = event.timeStamp - last_clicked;
    
        last_clicked = event.timeStamp;
    
        if(time_since_clicked < 2000)
          return false;
    
        return true;
      });   
    };
    

    Using like this:

    $('#my-form').preventDoubleSubmission();
    

    I found that the solutions that didn't include some kind of timeout but just disabled submission or disabled form elements caused problems because once the lock-out is triggered you can't submit again until you refresh the page. That causes some problems for me when doing ajax stuff.

    This can probably be prettied up a bit as its not that fancy.

    0 讨论(0)
  • 2020-11-22 08:24

    Change submit button:

    <input id="submitButtonId" type="submit" value="Delete" />
    

    With normal button:

    <input id="submitButtonId" type="button" value="Delete" />
    

    Then use click function:

    $("#submitButtonId").click(function () {
            $('#submitButtonId').prop('disabled', true);
            $('#myForm').submit();
        });
    

    And remember re-enable button when is necesary:

    $('#submitButtonId').prop('disabled', false);
    
    0 讨论(0)
提交回复
热议问题