admin管理员组

文章数量:1414621

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?

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?

Share Improve this question asked Jun 5, 2018 at 20:46 BitwiseBitwise 8,47125 gold badges80 silver badges177 bronze badges 1
  • I cannot replicate this: jsfiddle/khrismuc/2zo6uev5 – user5734311 Commented Jun 5, 2018 at 21:05
Add a ment  | 

3 Answers 3

Reset to default 5

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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <button class="button">Submit</button>
</form>

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.

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

本文标签: javascriptdisabling button on click stop form from submittingStack Overflow