I need to disable a button once it's clicked so the user can't click it more than once. (My application is written in MVC ASP.NET, I've done this in a normal ASP.NET application.)
I tried using JavaScript and jQuery and it's not working. The button is getting disabled but the form is not being submitted.
jQuery:
$("#ClickMe").attr("disabled", "disabled");
JavaScript:
function DisableNextButton(btnId) { document.getElementById(btnId).disabled = 'true'; }
Both methods work great, but now the form won't submit.
1.1 To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); 1.2 To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.
Flutter pushes declarative UI to its limit in this case. Enable and disable the state of a button resulting from the present of onPressed callback. If the onPressed callback is null , Flutter treat the button as no action hence showing the button in a disabled state.
To disable a button using only JavaScript you need to set its disabled property to false . For example: element. disabled = true . And to enable a button we would do the opposite by setting the disabled JavaScript property to false .
jQuery now has the .one()
function that limits any given event (such as "submit") to one occurrence.
Example:
$('#myForm').one('submit', function() { $(this).find('input[type="submit"]').attr('disabled','disabled'); });
This code will let you submit the form once, then disable the button. Change the selector in the find() function to whatever button you'd like to disable.
Note: Per Francisco Goldenstein, I've changed the selector to the form and the event type to submit. This allows you to submit the form from anywhere (places other than the button) while still disabling the button on submit.
Note 2: Per gorelog, using attr('disabled','disabled')
will prevent your form from sending the value of the submit button. If you want to pass the button value, use attr('onclick','this.style.opacity = "0.6"; return false;')
instead.
If when you set disabled="disabled" immediately after the user clicks the button, and the form doesn't submit because of that, you could try two things:
//First choice [given myForm = your form]: myInputButton.disabled = "disabled"; myForm.submit() //Second choice: setTimeout(disableFunction, 1); //so the form will submit and then almost instantly, the button will be disabled
Although I honestly bet there will be a better way to do this, than that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With