开发者

Choose selectbox option by javascript by text [duplicate]

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜