0

So I want to enable a submit button whenever an option in my select box (#purchase) is selected (so long as the selection is not the default value.

Here is the jquery code I put together, which is apparently not very good. :)


    $(document).ready(function(){
     $('input[type=\"submit\"]').attr('disabled','disabled');
                 $('#purchase').change(function(){

                         $('input[type=\"submit\"]').attr('disabled', 'enabled');

                 });
             });  

Here is my simple little form...


    <input type='submit' id='submit' value='Purchase'>                                                 
    <select name='purchase' id='purchase'>  
    <OPTION value='default' DEFAULT>default</OPTION>                                                 
    <OPTION value='small'>11 x 14" Print - $17.00</OPTION>
    <OPTION value='big'>20 x 30" Print - $40.00</OPTION>
    </select> 

Can anybody give me a push in the right direction? :)

Thanks!

1
  • By the way: you can omit the quotes and the backslashes in the selector: $('input[type=submit]') Commented Nov 18, 2009 at 9:24

2 Answers 2

2

To disable you can use

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

and for enabling

$('#submit').removeAttr('disabled');
Sign up to request clarification or add additional context in comments.

Comments

0
$(document).ready(function(){
     $('input[type=\"submit\"]').attr('disabled','disabled');
                 $('#purchase').change(function(){
                      if($('#purchase').val() != 'default') {
                         $('input[type=\"submit\"]').removeAttr('disabled');
                      }

                 });
             });  

Although, you could probably split the method out into it's own function, and set the button disabled from the start without using document ready.

$(document).ready(function(){ $('#purchase').bind('change', 'EnableSubmit'); });

function EnableSubmit() {
    if($('#purchase').val() != 'default') {
        $('input[type=submit]').removeAttr('disabled');
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.