开发者

Choose selectbox option by javascript by text [duplicate]

开发者 https://www.devze.com 2023-03-05 10:33 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Selecting an option by its text
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Selecting an option by its text

Is there any way how to choose the option in selectbox by th开发者_JAVA百科e text label of the option (not the value)?

<select id="form-mySelect" name="number">
    <option value="0">Zero</option>
    <option value="1">One</option>
    <option value="2">Two</option>
</select>


$("#form-mySelect").val("One");

The example above doesn't work in jQuery 1.4.3. I've read somewhere, that in some lower version of jQuery selecting option by >text label< (not by the value) works.

Is there any way, how to simulate this behavior and make it functional in jQuery 1.4.3?


You can use $('#form-mySelect").html() to get the label contents. So you could loop all options and compare the html() value with one given and then select the option.

0

精彩评论

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

关注公众号