开发者

Set selected using jQuery

开发者 https://www.devze.com 2023-02-01 19:10 出处:网络
I\'m trying to change the selected option within this list.. Its only working for some and not others?

I'm trying to change the selected option within this list.. Its only working for some and not others?

selectedVal will either be Kelly Green, Navy etc...

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

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

This is the select list:

<select class="single-option-selector" id="product-varian开发者_运维百科ts-option-0">
<option value="Gunmetal Heather">Gunmetal Heather</option>
<option value="Kelly Green">Kelly Green</option>
<option value="Navy">Navy</option>
</select>


Use value attribute instead of text in your selector. i.e:

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


There's a much simpler way to set the selected option of a <select> element.

Just call jQuery's .val() method against the <select> itself:

$("#product-variants-option-0").val( selectedVal );
0

精彩评论

暂无评论...
验证码 换一张
取 消