How to disable submit button once it has been clicked?

后端 未结 18 1562
轻奢々
轻奢々 2020-12-01 05:06

I have a submit button at the end of the form.

I have added the following condition to the submit button:

onClick=\"this.disabled=true;
this.value=\'         


        
18条回答
  •  囚心锁ツ
    2020-12-01 05:28

    Here's a drop-in example that expands on Andreas Köberle's solution. It uses jQuery for the event handler and the document ready event, but those could be switched to plain JS:

    (function(document, $) {
    
      $(function() {
        $(document).on('click', '[disable-on-click], .disable-on-click', function() {
          var disableText = this.getAttribute("data-disable-text") || 'Processing...';
    
          if(this.form) {
            this.form.submit();
          }
    
          this.disabled = true;
    
          if(this.tagName === 'BUTTON') {
            this.innerHTML = disableText;
          } else if(this.tagName === 'INPUT') {
            this.value = disableText;
          }
        });
      });
    
    })(document, jQuery);
    

    It can then be used in HTML like this:

    
    
    
    

提交回复
热议问题