我有一个下拉框,如下所示:
<select id="select_Boiler">
<option value="boiler_1645">Vaillant 831</option>
<option value="boiler_2373">Worcester 24</option>
<option value="boiler_3009">Vaillant 835</option>
<option value="boiler_4354">Bosch 671</option>
</select>我需要能够删除特定的选项使用jQuery,但基于文本,而不是选项值。我试过了,但没有成功:
jQuery("#select_Boiler option[text='Vaillant 835']").remove();我知道我可以用下面的值做同样的事情,它是有效的,但我需要通过文本来做
jQuery("#select_Boiler option[value='boiler_3009']").remove();发布于 2014-07-09 23:02:15
您可以使用:contains根据元素的文本内容进行过滤,但请注意,它将返回部分匹配。因此:contains('Vaillant 835')将返回:contains('Vaillant 835')和:contains('Vaillant 8356')
jQuery("#select_Boiler option:contains('Vaillant 835')").remove();如果你想进行相等的过滤,你需要像这样手动过滤
jQuery("#select_Boiler option").filter(function(){
return $.trim($(this).text()) == 'Vaillant 835'
}).remove();发布于 2019-01-30 05:08:40
下面是Arun P Johny的答案中不区分大小写的选项。
jQuery("#select_Boiler option").filter(function() {
cur_text = $(this).text().trim().toLowerCase();
return cur_text.indexOf('Vaillant 835') != -1;
}).remove();https://stackoverflow.com/questions/24657154
复制相似问题