首页 > 代码库 > js 默认选中select 选项

js 默认选中select 选项

 

<select id="HDname" style="width: 150px;">
<option value="http://www.mamicode.com/0">请选择</option>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<option value="http://www.mamicode.com/<%#Eval("code") %>"><%#Eval("name") %></option>
</ItemTemplate>

</asp:Repeater>
</select>

 

<script>
var code = "<%=name%>";


if (code != "") {
setHDname(code);
}
//document.writeln(document.getElementById("sel").options.length);

//document.writeln(document.getElementById("sel")["options"].length);

//document.writeln(document.getElementById("sel").children.length);

function setHDname(code) {
var s1 = document.getElementById("HDname");

for (i = 0; i <= s1.options.length; i++) {
if (s1.options[i].value =http://www.mamicode.com/= code) {
s1.options[i].selected = true;
}

}
}
</script>

 

js 默认选中select 选项