How to disable multiple select options

南笙酒味 提交于 2019-12-20 03:53:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!