我试图在选项文本上找到选项索引库。我的HTML代码如下。
<select multiple="multiple" onchange="dependentOptions.select(this); opConfig.reloadPrice();optionImages.showImage(this);" title="" class="multiselect product-custom-option" id="select_472" name="options[472][]" >
<option selected="selected" value="3364">Black </option>
<option selected="selected" value="3365">White </option>
<option value="3366">Cool Grey #9 </option>
<option value="3367">Red </option>
<option value="3368">Fire Red </option>
<option value="3369">Orange </option>
<option value="3370">Rich Yellow </option>
<option value="3371">Primrose Yellow </option>
<option value="3372">Light Green </option>
</select>
我的脚本就像这样
jQuery("#select_472 select option:[text=Rich Yellow]").index();
但是它让我回归-1而不是7
所以请帮助我来制作它。
答案 0 :(得分:3)
您可以使用:contains
伪选择器:
jQuery("#select_472 option:contains('Rich Yellow')").index();
答案 1 :(得分:1)
索引应该从0开始,因此文本'Rich Yellow'是索引位置是6。
$("#select_472 option[value='3370']").index();
$("#select_472 option:contains('Rich Yellow ')").index();
答案 2 :(得分:1)
你可以过滤它(和修剪文本值),看起来更安全:
jQuery("#select_472 option").filter(function(){
return $.trim($(this).text()) === "Rich Yellow"
}).index();