Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling the button after once click

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.

like image 550
batwadi Avatar asked Feb 24 '10 05:02

batwadi


People also ask

How do I make a button disabled after one click?

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.

How do I disable the button after one click flutter?

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.

How do you set a button to disable?

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 .


2 Answers

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.

like image 164
Preston Badeer Avatar answered Oct 17 '22 18:10

Preston Badeer


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.

like image 21
Warty Avatar answered Oct 17 '22 19:10

Warty