在下拉菜单中隐藏选项 select

Hide options in dropdown select

我无权调整下拉列表中的选项。所以我想从 DOM 中隐藏它们。我在隐藏选项时遇到了麻烦。目前我可以隐藏整个下拉列表,如果我的数组包含我想隐藏的国家。

https://jsfiddle.net/gmLa5bgz/

html

<select id="country_2">
<option value="AS">American Samoa</option><option value="AD">Andorra</option><option value="AO">Angola</option><option value="AI">Anguilla</option>
</select>

JQuery

var countries = new Array("France","Germany");
var i = 0;
var countryLength = countries.length;
for(i = 0; i < countryLength; i++) {
$('#country_2').each(function(){
if($(this).text().search(countries[i]) >= 0)
{$(this).remove();};
});
}

试试这个:

var countries = new Array("France","Germany");
var i = 0;
var countryLength = countries.length;
for(i = 0; i < countryLength; i++) {
    $('#country_2 option').each(function(){
        if($(this).text()==countries[i])
        {
           $(this).remove();
        }
    });
}