Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disabling button on click stop form from submitting

I'm trying to disable a button after a user clicks it. I have this functionality working great but now the form wont submit... Here is my javascript:

<script>
  $('.button').on('click', function(event) {
    $(this).prop('disabled', true);
  });
</script>

Again this disables the button nicely but now my form wont submit. Is this a well known problem?

like image 895
Bitwise Avatar asked Jan 28 '26 19:01

Bitwise


2 Answers

Instead of putting it in the click handler, put it in the form's submit handler.

$('form').on('submit', function(event) {
  $(this).find(".button").prop('disabled', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <button class="button">Submit</button>
</form>
like image 84
Barmar Avatar answered Jan 30 '26 11:01

Barmar


Click event occurs before form submit and disabled button prevents submission. This is workaround:

<script>
  $('.button').on('click', function(event) {
    $this=$(this);
    setTimeout(function(){$this.prop('disabled', true);},50);
  });
</script>

Another workaround is to process $('form').on('submit',function(){...});

Edit: ...INSTEAD.

like image 20
Alex Kudryashev Avatar answered Jan 30 '26 10:01

Alex Kudryashev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!