开发者

How to set first option's value of Select Box using jQuery?

开发者 https://www.devze.com 2023-01-05 21:06 出处:网络
I have a select lis开发者_运维知识库t: <select id=\'myList\'> <option value=\"\">zero</option>

I have a select lis开发者_运维知识库t:

<select id='myList'> 
        <option value="">zero</option> 
        <option value="1">one</option> 
        <option value="2">two</option> 
        <option value="3">three</option> 
</select>

How can i set the value of option value to = 0 using jQuery?

I tried $("#myList > option").attr("value", "0");

But this changed them all,

I also tried $("#myList:first-child").attr("value", "0"); but this breaks the select box.

Any ideas?

Thanks, Kohan.


$("#myList > option:first").attr("value", 0);

use :first to filter

you could also,

$("#myList :first-child").attr("value", 0); // watch out for the space in the selector


Try this:

$("#myList option[value='']").attr("value", "0");
0

精彩评论

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

关注公众号