通过jquery,设置为html-select元素的选项,按文本而非值选择属性 [英] by jquery, set to option of html-select element selected atribude by text, not value
本文介绍了通过jquery,设置为html-select元素的选项,按文本而非值选择属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个选择元素.
<select class='cSelectType'>
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
<option value="5">five</option>
</select>
我可以通过jquery设置选择的选项值,如下所示:
i can by jquery set option value selected like this:
$('.cSelectType option[value=4]').attr('selected', 'selected');
并且选择元素将显示四个".通过我我不会这样的事情:
And select element will show "Four". By i wont something like this:
$('.cSelectType option[xxx=Four]').attr('selected', 'selected');
即按文本设置所选属性.
i.e By text set selected attribute.
请一些想法.
推荐答案
使用 :contains()
过滤器:
Use the :contains()
filter:
说明:选择所有包含指定文本的元素.
Description: Select all elements that contain the specified text.
$(".cSelectType option:contains('four')").attr('selected', 'selected');
这篇关于通过jquery,设置为html-select元素的选项,按文本而非值选择属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文