Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable submit button on form submit

I wrote this code to disable submit buttons on my website after the click:

$('input[type=submit]').click(function(){     $(this).attr('disabled', 'disabled'); }); 

Unfortunately, it doesn't send the form. How can I fix this?

EDIT I'd like to bind the submit, not the form :)

like image 696
markzzz Avatar asked Apr 17 '11 02:04

markzzz


People also ask

How do you disable submit button on form submit?

Enable / Disable submit button 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 disable submit button until form is filled jQuery?

click(function () { if ($('#submit-button').is(':disabled')) { $('#submit-button'). removeAttr('disabled'); } else { $('#submit-button'). attr('disabled', 'disabled'); } }); In the above code, enabled is the id of the checkbox used and submit-button is the class name of the submit button used .


1 Answers

Do it onSubmit():

$('form#id').submit(function(){     $(this).find(':input[type=submit]').prop('disabled', true); }); 

What is happening is you're disabling the button altogether before it actually triggers the submit event.

You should probably also think about naming your elements with IDs or CLASSes, so you don't select all inputs of submit type on the page.

Demonstration: http://jsfiddle.net/userdude/2hgnZ/

(Note, I use preventDefault() and return false so the form doesn't actual submit in the example; leave this off in your use.)

like image 177
Jared Farrish Avatar answered Sep 20 '22 05:09

Jared Farrish