$('select').on('change', function(){
$('select option').prop("disabled", false);
$("select").not(this).find("option[value="+ $(this).val() + "]").prop('disabled', true);
});
I have tow drop-down
<select name="start_year" id="start_year">
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
</select>
<select name="last_year" id="last_year">
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
</select>
I want if i choose 2016 in my first drop-down then 2015 and 2014 should disable in second drop-down.
The solution is to
disable
options in secondselect
which whose values are only less thenstart_year
select value. You have to iterateoptions
fromend_year
select, and setdisabled
property only for those which respecting the property:if($(this).val()<value)
.I assume you want this to work both ways. Select a year in the second dropdown disables the lower starting years in the first dropdown.
If not, just remove the change event bound to
#last_year
.It is also prudent to have a default option included, either empty or with some default text.
jsfiddle, also runnable snippet is below: