我无权调整下拉列表中的选项。所以我想把它们从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();};
});
}
答案 0 :(得分:2)
试试这个:
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();
}
});
}