Jquery prevent multiple submit

后端 未结 7 831
花落未央
花落未央 2020-12-30 09:58

I want to prevent multiple submit if someone click on one of the submit buttons multiple times.

How can unbind or undelgate in this case th

相关标签:
7条回答
  • 2020-12-30 10:35

    There are two ways of doing this as pointed out by cHao.

    $('form button').prop('disabled', true);
    

    or

    $('form button').attr('disabled', 'disabled');
    
    0 讨论(0)
  • 2020-12-30 10:37

    If you are using ASP.NET MVC Data Annotations for Client side Validation, you have to validate the form first -

    // To prevent multiple post
    function buttonsubmit() {
        $('form').submit(function () {
            if ($('form').valid()) {
                $(this).find("input[type='submit']").prop('disabled', true);
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-30 10:39

    If you have users who insist on having an active submit button all the time you can use a variable to track the progress. Something like this:

    saving_record = 0; //set variable to show if form is in submission
    $(document).ready(function () {
        $('#form').on('submit', function (e) {
            if (saving_record === 0) { //If not submitted yet, submit form
                saving_record = 1; //Set variable to show form is in submission
                $.ajax({
                    success: function (data) {
                        saving_record = 0; //Reset variable to allow record update
                    },
                    error: function (data) {
                        saving_record = 0; //Reset variable to allow record update in case of error
                    }
                })
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-30 10:40
    $(document).ready(function(){
      $('div.ajax form').submit(function(){
         $(this).do_some_stuff();
         return false;
       });          
    });
    
    0 讨论(0)
  • 2020-12-30 10:43

    This works on submit

    $("form").submit(function() {
        // submit more than once return false
        $(this).submit(function() {
            return false;
        });
        // submit once return true
        return true;
    });
    
    0 讨论(0)
  • 2020-12-30 10:44

    when you click the button within the function add the attr(disabled) which will make the button inactive.

    $("form Button").attr("disabled","1"); 
    

    should prevent multiple submissions

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