问题描述
我需要检查<select>
是否具有文本等于特定值的选项.
I need to check if a <select>
has an option whose text is equal to a specific value.
例如,如果有一个<option value="123">abc</option>
,我会在寻找"abc".
For example, if there's an <option value="123">abc</option>
, I would be looking for "abc".
是否有选择器可以做到这一点?
Is there a selector to do this?
我正在寻找与$('#select option[value="123"]');
类似的内容,但需要输入文字.
Im looking for something similar to $('#select option[value="123"]');
but for the text.
推荐答案
这可能会有所帮助:
$('#test').find('option[text="B"]').val();
这将为您提供文本为B
的选项,而不是文本为包含B
的选项.希望这会有所帮助
This would give you the option with text B
and not the ones which has text that contains B
. Hope this helps
对于jQuery的最新版本,以上操作无效.如下面的 Quandary 所述,这才适用于jQuery 1.9.1:
For recent versions of jQuery the above does not work. As commented by Quandary below, this is what works for jQuery 1.9.1:
$('#test option').filter(function () { return $(this).html() == "B"; }).val();
这篇关于我需要哪个选择器通过其文本选择一个选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!