How to set the default value of a select box using JQuery in IE9?

I have the following select box.

<select id="selId">
     <option id='1' value='1'>1</option>
     <option id='2' value='2'>2</option>
     <option id='3' value='3'>3</option>
     <option id='4' value='4'>4</option>
     <option id='5' value='5'>5</option>
</select>

In jquery I am doing the following to select the value 2 in the select box.:

...
$("select#selId").find("option#2").attr("selected", "selected");
...

The same code sets the value of 2 in the select box in IE8 and Firefox. But its not working in IE9.

I am using JQuery 1.6.1 version


Instead of setting the selected attribute, just use .val("2").

See here: jsFiddle


Anyway, this worked well on IE9 if you want to mantain the "SELECTED" attribute

$("select#selId").find("option#2").attr("selected", true);

http://jsfiddle.net/cqENs/


您可以使用val()来设置选项 - 请参阅使用jQuery更改下拉列表的选定值

链接地址: http://www.djcxy.com/p/83924.html

上一篇: jquery相关的下拉框填充

下一篇: 如何在IE9中使用JQuery设置选择框的默认值?