Frankie Frankie - 2 months ago 8
Javascript Question

Disable 3th, 4th dropdown list if 1st or 2nr are selected

I have this issue: In my form there are 4 dropdownlist and when the 1st (category1) or 2nd (software1) dropdown list is selected, the 3th (category2) and 4th (software2) must be disabled.

For this issue I find this script at disable-second-dropdown-if-the-first-is-not-selected but I do not trust to modify this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
category1
<select name='cat1'>
<option value='0'>Select one</option>
<option value='1'>little</option>
<option value='2'>good</option>
</select>

software1
<select name='soft1'>
<option value=''>Select one</option>
<option value='W'>Word</option>
<option value='E'>Excel</option>
<option value='PP'>Power Point</option>
</select>

<br />
category2
<select name='cat2'>
<option value='0'>Select one</option>
<option value='1'>little</option>
<option value='2'>good</option>
</select>

software2
<select name='soft2'>
<option value=''>Select one</option>
<option value='W'>Word</option>
<option value='E'>Excel</option>
<option value='PP'>Power Point</option>
</select>

<script type="text/javascript">
var setEnabled = function(e) {
var name = this.name.replace(/1/, '2'); //get name for second drop down
$('select[name=' + name + ']')
.prop('disabled', 0 === this.selectedIndex) // disable if selected option is first one
};

$(function() {
$('select[name=cat1], select[name=soft1]')
.on('change', setEnabled)
.trigger('change'); // trigger on page load
});
</script>


How to modify this?

Thanks

Answer

I think the main thing you want is to flip === for !==

However, to get this working on a matrix, where both top inputs trigger enabling/disabling of both bottom inputs, you'll need to test both on change of either.

var setEnabled = function(e) {
  var selected = $('select[name=cat1]').prop('selectedIndex') > 0 || $('select[name=soft1]').prop('selectedIndex') > 0;
  $('select[name=cat2], select[name=soft2]').prop('disabled', selected); // disable if selected option is first one
  if (selected) {
    $('select[name=cat2], select[name=soft2]').prop('selectedIndex', 0)
  }
};

$(function() {
  $('select[name=cat1], select[name=soft1]')
      .on('change', setEnabled)
      .trigger('change'); // trigger on page load
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</script>
category1
<select name='cat1'>
  <option value='0'>Select one</option>
  <option value='1'>little</option>
  <option value='2'>good</option>
</select>

software1
<select name='soft1'>
  <option value=''>Select one</option>
  <option value='W'>Word</option>
  <option value='E'>Excel</option>
  <option value='PP'>Power Point</option>
</select>

<br />
category2
<select name='cat2'>
  <option value='0'>Select one</option>
  <option value='1'>little</option>
  <option value='2'>good</option>
</select>

software2
<select name='soft2'>
  <option value=''>Select one</option>
  <option value='W'>Word</option>
  <option value='E'>Excel</option>
  <option value='PP'>Power Point</option>
</select>