1

I have a simple form with four select fields. If YES is selected on any form field, the submit button needs to be disabled and a hidden div should appear. Here's an example of my markup:

<form>
I have read the information on the product(s)<select name="field4"> <option value="Yes">Yes</option> <option value="No">No</option> </select>

Do you have any allergies to any ingredients in product(s)?
 <select name="field5"><option value="Yes">Yes</option> <option value="No">No</option> </select>

Are you pregnant?
 <select name="field6"> <option value="Yes">Yes</option> <option value="No">No</option> </select>

Have you ever used this type of product and had undesirable results?
 <select name="field7"> <option value="Yes">Yes</option> <option value="No">No</option> </select>

<input name="cmdSubmit" type="submit" value="Submit" />
</form>


<div style="display:none;">Hidden div only to appear if any of the four dropdowns are marked YES.</div>

Thanks for your help on this one. I am a jquery novice trying my best to learn.

1
  • What have you tried so far? I recommend to give all selectboxes a specific class so the selection will be easier. Commented Dec 1, 2010 at 16:46

4 Answers 4

5

I don't completely understand the business logic of the way you have the form built and your requirements but... going based on what you have said here is what I would do.

  1. write function to check all options, change disable/enable submit, show/hide div
  2. call function on load
  3. call function to check all options every time an option value changes

.

checkOptions();
$("select").change(checkOptions);

function checkOptions() {
  var yesFound = false;
  $("select").each(function(index, element) {
    if ( $(element).val() == "Yes" ) {
      yesFound = true;
    }
  });

  if (yesFound) {
    $("#hidden-div").show();
    $("input[type=Submit]").attr("disabled","disabled");
  } else {
    $("#hidden-div").hide();
    $("input[type=Submit]").removeAttr("disabled");
  };
}

with slightly modified HTML:

<form>
  <select name="field4"> 
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select> I have read the information on the product(s)<br/>
  <select name="field5">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select> Do you have any allergies to any ingredients in product(s)?<br/>
  <select name="field6">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select> Are you pregnant?<br/>
  <select name="field7">
    <option value="Yes">Yes</option>
    <option value="No">No</option>
  </select> Have you ever used this type of product and had undesirable results?<br/>
  <br/>
  <input name="cmdSubmit" type="submit" value="Submit" />
</form>
<div id="hidden-div" style="display:none;">Hidden div only to appear if any of the four dropdowns are marked YES.</div>

Example webpage:

http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-disable-submit-dynamically.html

On load:

alt text

all options 'No' but one:

alt text

all options 'Yes':

alt text

Sign up to request clarification or add additional context in comments.

2 Comments

yours are some of the most thorough answers I've seen on SO. It's like you're doing people's work for them! :-)
@Andrew, haha! Thanks! I try to answer questions with a working example because sometimes seeing whole working code can make all the difference.
1

Give all your selects (dropdowns) a class "foo". Give the options that are "Yes" a class "yes". $("select.foo option.yes:selected").empty() means "None of the dropdown options marked "yes" are selected"

So

$("select.foo").change(function() {
   if(!$("select.foo option.yes:selected").empty()) {
      /* Do something here, maybe use BlockUI JS library */
   }
});

4 Comments

Naturally you may want to figure out a better name than "foo" :)
As per buddhabrot's answer above, "Give the options that are "Yes" a class "yes"."
This is a dropdown not checkboxes
Note that using a class for the options means you are not bound to the term "Yes", which may be translated into a different language. I would advise against using text like that as some of the other answers do.
0
var selects = $('select').change(function(){ //on change of any select
    var hasYes = false;
    selects.each(function(){ // check all selects
        if($(this).val() == 'Yes') //for 'Yes'
            hasYes = true;
    });
    if(hasYes){ // show div disable submit
       $('input:submit').attr('disabled','disabled');
       $('div.hidden').show();
    }
    else{
        $('input:submit').removeAttr('disabled');
       $('div.hidden').hide(); // add class hidden to the div
    }
});

Comments

0

You could set the class attribute on both your selects to something like "choices" like this:

<select name="field5" class="choices"...

<select name="field6" class="choices"...

and do something like this:

$(function() {
  $(".choices").change(function() {
    var anyYeses = false;
    $(".choices").each(function() {
      if($(this).val() == "Yes") {
        anyYeses = true;
      }
    });
    if(anyYeses) {
      $("input[name=cmdSubmit]").attr('disabled', 'disabled'); //disables
    }
    else {
      $("input[name=cmdSubmit]").removeAttr('disabled'); //enables
    }
  });
});

4 Comments

This only works for one dropdown at a time. And what if they go back to no?
@Josiah Ruddell, this will work for all of them if you update the class attribute on all select elements. Please see my updates.
But only one at a time. what if they select yes in one dropdown, then yes in another dropdown, then no in the first dropdown. They still have 1 dropdown with yes but the button will be reenabled.
@Josiah Ruddell, try that change out.

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.