问题
How to disable multiple options in one select. For example i have 2 selects with 5 and 3 options. So if i select 3rd option in second select, i want to disable 3rd,4th and 5th option in first select:
<select name="smjer">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
Second select:
<select name="godina" >
<option value="1" >1.</option>
<option value="2" >2.</option>
<option value="3">3.</option>
</select>
I've tried removing options, but then I can't retrieve them without refreshing page:
var ary=[3,4,5];
$('[name=smjer] option').filter(function(){
return ($.inArray(parseInt(this.value),ary) >-1);
}).remove();
回答1:
You can just hide them :
var ary = [3, 4, 5];
$('[name=smjer] option').filter(function () {
return ($.inArray(parseInt(this.value), ary) > -1);
}).hide();
and when again required show it:
var ary = [3, 4, 5];
$('[name=smjer] option').filter(function () {
return ($.inArray(parseInt(this.value), ary) > -1);
}).show();
To make it even better, you can do this:
var ary = [3, 4, 5];
// Get the options to be hidden/shown
var $options = $('[name=smjer] option').filter(function () {
return ($.inArray(parseInt(this.value), ary) > -1);
});
// Hide the options
$options.hide();
// Show the options
$options.show();
回答2:
Also you can disable them:
var ary=[3,4,5];
$('[name=smjer] option').filter(function(){
return ($.inArray(parseInt(this.value),ary) >-1);
}).prop('disabled', true);
来源:https://stackoverflow.com/questions/16069204/how-to-disable-multiple-select-options