views:

42

answers:

4
<select name="feature1">
  <option value="1">Enable</option>
  <option value="0">Disable</option>
</select>

<input type="checkbox" name="feature2" />
<input type="checkbox" name="feature3" />

How do I disable 'feature2' and 'featured3' inputs when 'disable' in 'feature1' is selected?

Thanks

+2  A: 

This automatically enables the items again when 'Enable' is selected, which I assume is also what you want.

$('select[name=feature1]').change(function() {
   $('input[name=feature2],input[name=feature3]').attr('disabled', $(this).val() == '0');
});
David Hedlund
A: 

Add some ids for your elements, then:

$('#feature1').change(function() {
  if(this.value === "0") {
    $('#feature2').attr('disabled', 'disabled');
    $('#feature3').attr('disabled', 'disabled');
  }
});
galambalazs
the value will never be `=== 0`, as it's a string. also, some old versions doesn't have `this.value` (back in the day, you had to write `this[this.selectedIndex].value`), so for compatibility, i'd recommend `$(this).val()` or checking `selectedIndex == 1` instead of comparing value. also, they're not IDs, and feature3 is left unaffected.
David Hedlund
i mistyped the string. value is more compatible than jQuery would ever get. feature3 was there before you finished the comment. :)
galambalazs
+1  A: 

I'm going to assume you have ID's the same as your element names, for simplicity

jQuery('#feature1').change(function() {
   jQuery("#feature2, #feature3").attr("disabled", jQuery(this).val() == '0'); 
});
Gareth Midwood
A: 
<script type="text/javascript">
 abc(val){
    if (val.value=="0"){
      document.getElementByID("").disabled= true;
      document.getElementByID("").disabled= true;
    }else{
      document.getElementByID("").disabled= false;
      document.getElementByID("").disabled= false;
    }
  }

</script>

<select name="feature1" onchange="abc(this)">
  <option value="1">Enable</option>
  <option value="0">Disable</option>
</select>

<input type="checkbox" name="feature2" id="feature2" />
<input type="checkbox" name="feature3" id="feature3" />
Salil
sorry i didn't see the jquery TAG but i don't delete answer for the Record.
Salil