Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable submit button with jquery

Tags:

jquery

I have a simple form to check the value of post code entered by the user against a variable,

I'm trying to disable the submit button and do the check and give alert message, I can't figure out what I'm doing wrong, any help would be appreciated.

  <form id="post_code">         <input name="textfield" type="text" id="postcode" size="14" maxlength="8" />         <input type="image" id="submit_postcode" src="images/B_go.png" alt="Submit Postcode" width="59" height="24" border="0" />   </form>   $(function() {     $('form#post_code').submit(function() {      var entered_post_code = $('form#post_code input#postcode').val();     var post_code = "CV";     if (entered_post_code == post_code) {         alert("post code is ok");         return true;     }else {         alert("post code is not ok");         return true;     }     return false;    }); 

});

like image 535
akano1 Avatar asked Aug 06 '09 09:08

akano1


People also ask

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'); } });

Is button disabled JQuery?

Checking if the button is disabled or enabled with jQuery removeAttr('disabled'); }; const checkButton = (e) => { const isDisabled = $('#my-button'). prop('disabled'); if( isDisabled ){ alert("Is disabled!"); } else{ alert("Is enabled"); } }; $(document).

How do I stop a form from submitting in JQuery?

We use the preventDefault() method with this event to prevent the default action of the form, that is prevent the form from submitting.


2 Answers

The W3C recommends to set that attribute to disabled="disabled", so:

$('#submit_postcode').attr('disabled', 'disabled'); 

Re-enabling can be done by removing the attribute:

$('#submit_postcode').removeAttr('disabled'); 

Setting the attribute to any value causes it to be disabled, even .attr('disabled', 'false'), so it has to be removed.

like image 59
Mythica Avatar answered Sep 19 '22 23:09

Mythica


$('#submit_postcode').attr('disabled', 'disabled'); 
like image 43
David Avatar answered Sep 18 '22 23:09

David