How to Prevent Users from Submitting a Form Twice

后端 未结 16 996
挽巷
挽巷 2020-12-01 06:59

when user click add button twice, from get submitted twice with same dat

16条回答
  •  庸人自扰
    2020-12-01 07:21

    Create a class for the form, in my case I used: _submitlock

    $(document).ready(function () {
      $(document).on('submit', '._submitlock', function (event) {
    
          // Check if the form has already been submitted
          if (!$(this).hasClass('_submitted')) {
              // Mark the form as submitted
              $(this).addClass('_submitted');
    
              // Update the attributes of the submit buttons
              $(this).find('[type="submit"]').attr('disabled', 'disabled');
              // Add classes required to visually change the state of the button
              $(this).find('[type="submit"]').addClass("buttoninactive");
              $(this).find('[type="submit"]').removeClass("buttonactive");
    
          } else {
              // Prevent the submit from occurring.
              event.preventDefault();
          }
    
      });});
    

提交回复
热议问题