开发者

jQuery Appending Attribute to Elements That Contain The Specified Text Node

开发者 https://www.devze.com 2023-01-16 11:49 出处:网络
For example, I would like to append selected=\"selected\" to the op开发者_JAVA百科tion that contains This one, which jQuery selector should be used to append attributes into that element?

For example, I would like to append selected="selected" to the op开发者_JAVA百科tion that contains This one, which jQuery selector should be used to append attributes into that element?

<select>
    <option>Not this</option>
    <option>This one</option>
</select>

Thanks in advance!


If there are no value attributes, then the text becomes the value, so you can just use .val():

$("select").val("This one");

Or, if it has a value="something" attribute, use that instead:

$("select").val("something");
0

精彩评论

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