2

I have a code that disables the button after one click perfectly, but the problem is, is the form doesn't submit then. How do I disabled the button after one click and still have the form submit.

Code

<input id="submitbtn" name="submit" type="submit" value="Submit for Payment" />

<script type="text/javascript">
    $(function(){
        $('#submitbtn').one('click', function() {  
        $(this).attr('disabled','disabled');
        });
    });
</script>

4 Answers 4

3

Try this:

$('form').submit(function() {
  $(this).find("button[type='submit']").prop('disabled',true);
});
Sign up to request clarification or add additional context in comments.

Comments

0

Deferring the button disabling code until after the form submit event fires might be your best option. You can do this a few ways. If you only have one button that you want to disable, add a submit event listener to the form and disable it in the callback. Another simple approach is to use setTimeout which runs after 0 milliseconds.

setTimeout( () => $(this).attr('disabled','disabled') , 0);

Also, if you're using a version of jQuery that supports .prop(), I'd recommend using that instead of attr. Example: $(this).prop('disabled', true).

1 Comment

Ok great, just keep in mind the asynchronous nature of doing it the setTimeout way. Also, take note of the context of this with relation to the arrow function (() => {}) as it is different than standard functions. More reading here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
0

$(function(){
  $('#submitbtn').on('click', function() {  
    $(this).prop('disabled', true);
    $("#formID").submit();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="submitbtn" name="submit" type="submit" value="Submit for Payment" />

3 Comments

The submit() function has to be called on a <form> not a submit button.
It should be $(this.form).submit(). But the button isn't inside a form.
Yeah. I wanted to actually use form ID. Thanks.
0

Add the onsubmit attribute to your form and put the code to disable the button there, so you disable the button but the first submit has already happened:

<form onsubmit="myFunction();">

<script type="text/javascript">
    function myFunction(){
        $('#submitbtn').attr('disabled','disabled');
        // my submit code here
    }
</script>

5 Comments

Where is myFunction() defined?
myFunction is the function where to put the code for disabling the button...I thought it was obvious just updating the answer to add it compelte.
That won't work. You have the code that disables the submit button inside an event handler. But the event handler won't be added until after the user clicks the submit button, so the code will never run until the user tries to submit the form a second time.
Get rid of the .one() function and just disable the submit button.
thanks I know, I just didn't have time to do that, just copied the code from inside the previous answer after received a comment about the function...I was just doing the same...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.