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

≯℡__Kan透↙ 提交于 2019-12-18 15:44:55

问题


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


回答1:


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

See here: jsFiddle




回答2:


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/




回答3:


You could use val() to set the option instead - see Change the selected value of a drop-down list with jQuery




回答4:


change this one

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

into this one

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


来源:https://stackoverflow.com/questions/6329580/how-to-set-the-default-value-of-a-select-box-using-jquery-in-ie9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!