滤波器选择其中[ATTR!=阵列](Filter selection where [attr!=Ar

2019-10-30 05:40发布

我有一个select与所在国家的值是2字符代码。

<select>
    <option value=""> </option>
    <option value="AF">Afghanistan</option>
    <option value="AX">Åland Islands</option>
    ...
    <option value="ZW">Zimbabwe</option>
</select>

我想禁用所有的不在多数民众赞成产生一个数组的选项。

var allowedCountries = ["AF", "AX", "AL", "DZ", "AS", "AD", "AO"];

有没有办法一起做这个.filter()或者我必须通过选项循环?

Answer 1:

$("option").filter(function() {
    return allowed_countries.indexOf(this.value) == -1;
}).prop('disabled', true);

你或许应该把""allowedCountries ,使他们可以取消菜单。 要么,或滤波之后重新启用它。



Answer 2:

你可以尝试使用inArrayfilter

$("option").filter(function (i) {
    return $.inArray(this.value, allowedCountries) == -1
}).prop("disabled", true);

演示: http://jsfiddle.net/hungerpain/cJVxC/1/



文章来源: Filter selection where [attr!=Array]