当前位置: 代码迷 >> JavaScript >> 页面传入ID,打开后如何让下拉菜单的值显示为selected
  详细解决方案

页面传入ID,打开后如何让下拉菜单的值显示为selected

热度:27   发布时间:2012-02-22 19:36:55.0
页面传入ID,打开后怎么让下拉菜单的值显示为selected
如果这个ID和下拉菜单的某一个选项的值相同,打开页面时下拉菜单默认显示的是这个选项,
请问用JS怎么做?

如:ID=5

<form   name= "form1 "   method= "post "   action= " ">
    <select   name= "select ">
<option   value= "5 "   selected> gf </option> //因为ID和选项的值相同就显示为:selected
<option   value= "4 "   > wq </option>
<option   value= "2 "   > we </option>
<option   value= "3 "   > sd </option>
<option   value= "1 "   > fd </option>
    </select>
</form>

------解决方案--------------------
<script>
function show()
{
var sel=document.getElementById( "select ");
var id=3;
sel.selectedIndex=id;
}
</script>

<body onload= "show(); ">
<form name= "form1 " method= "post " action= " ">
<select name= "select ">
<option value= "5 " selected> gf </option> //因为ID和选项的值相同就显示为:selected
<option value= "4 " > wq </option>
<option value= "2 " > we </option>
<option value= "3 " > sd </option>
<option value= "1 " > fd </option>
</select>
</form>
</body>
  相关解决方案