How to disable submit button once it has been clicked?

后端 未结 18 1358
轻奢々
轻奢々 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:19

    Your question is confusing and you really should post some code, but this should work:

    onClick="this.disabled=true; this.value='Sending...'; submitForm(); return false;"
    

    I think that when you use this.form.submit() it's doing what happens naturally when you click the submit button. If you want same-page submit, you should look into using AJAX in the submitForm() method (above).

    Also, returning false at the end of the onClick attribute value suppresses the default event from firing (in this case submitting the form).

    0 讨论(0)
  • 2020-12-01 05:20
     
        A better trick, so you don't lose the value of the button is
    
        function showwait() {
        document.getElementById('WAIT').style['display']='inline';
        document.getElementById('BUTTONS').style['display']='none';
        }
     

    wrap code to show in a div

    id=WAIT style="display:none"> text to display (end div)

    wrap code to hide in a div

    id=BUTTONS style="display:inline"> ... buttons or whatever to hide with
    onclick="showwait();" (end div)

    0 讨论(0)
  • 2020-12-01 05:20

    I think easy way to disable button is :data => { disable_with: "Saving.." } This will submit a form and then make a button disable, Also it won't disable button if you have any validations like required = 'required'.

    0 讨论(0)
  • 2020-12-01 05:24

    You need to disable the button in the onsubmit event of the <form>:

    <form action='/' method='POST' onsubmit='disableButton()'>
        <input name='txt' type='text' required />
        <button id='btn' type='submit'>Post</button>
    </form>
    
    <script>
        function disableButton() {
            var btn = document.getElementById('btn');
            btn.disabled = true;
            btn.innerText = 'Posting...'
        }
    </script>
    

    Note: this way if you have a form element which has the required attribute will work.

    0 讨论(0)
  • 2020-12-01 05:26

    Using JQuery, you can do this..

    $("#submitbutton").click(
       function() {
          alert("Sending...");
          window.location.replace("path to url");
       }
    );
    
    0 讨论(0)
  • 2020-12-01 05:27

    I don't think you need this.form.submit(). The disabling code should run, then it will pass on the click which will click the form.

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