jQuery:按值选择选项元素

时间:2011-04-13 03:11:00

标签: javascript jquery

如果值为“1”,我想从某些选择框中删除特定选项(所有选项都具有相同的值)。问题是,我很难将元素过滤到特定值。

目前,我最接近的是

$('.demoClass').find('option:contains(1)').remove();

但有两个问题。首先,这似乎迭代了选项的(显示的)内容,而不是它的值......其次,它似乎返回true,内容包含该值,而那个价值。 (例如,包含(1)将为(1,10,11,12)返回true ...当我希望它仅为“1”返回true时)

简而言之,我要做的是在所有选择“.demoClass”类框中找到值为X的时间段选项并删除它们。任何建议表示赞赏。

2 个答案:

答案 0 :(得分:4)

试试这个......

$('.demoClass').find('option[value="1"]').remove();

这会使用Attribute Equals Selector

from jquery

答案 1 :(得分:0)

试试这个:

$('.demoClass').find('option[value=1]').remove();