Man*_*tha 5 javascript jquery select
假设<select>表单中有许多元素.我需要一个选择器来选择一个<select>元素,其选择的选项具有特定的文本.为了解释,假设有5个<select>"颜色"类的元素.他们每个人都有3个<option>文本"白色","黑色","绿色".现在我需要选择所选选项为"白色"的元素.
<select class="color" >
<option value=""></option>
<option value="1"> white </option>
<option value="2"> black </option>
<option value="2"> green </option>
</select>
Run Code Online (Sandbox Code Playgroud)
在如下图所示的场景中,我需要选择那两个白色<select>.

谢谢.
尝试这个。
$("select.color option:selected:contains(white)").parent();
Run Code Online (Sandbox Code Playgroud)
这将为您提供所有具有以白色开头的文本的选择元素
注意:它也会匹配whitesmoke
编辑:以下代码将为您提供确切的值。
var requiredelements = $("select.color option:selected").filter(
function()
{
return ( $.trim($(this).text())=== 'white')
}).parent('select');
alert(requiredelements.length);
Run Code Online (Sandbox Code Playgroud)