日期:2014-05-16  浏览次数:20392 次

页面传入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>