使用jQuery设置选中

Cha*_*rsh 1 jquery select options selector attr

我正在尝试更改此列表中的所选选项..它仅适用于某些而非其他人?

selectedVal将是Kelly Green,Navy等......

 var selectedVal = $(this).text();

 $("#product-variants-option-0 option[text=" + selectedVal+"]").attr("selected","selected") ;
Run Code Online (Sandbox Code Playgroud)

这是选择列表:

<select class="single-option-selector" id="product-variants-option-0">
<option value="Gunmetal Heather">Gunmetal Heather</option>
<option value="Kelly Green">Kelly Green</option>
<option value="Navy">Navy</option>
</select>
Run Code Online (Sandbox Code Playgroud)

Cha*_*ndu 5

在选择器中使用值属性而不是文本.即:

$("#product-variants-option-0 option[value=" + selectedVal+"]").attr("selected","selected") ;
Run Code Online (Sandbox Code Playgroud)