用JS怎么给select标签设置动态的默认值

在jsp页面中,加载这个页面时会从数据库传来一条记录<%=staff.getSection() %>,要将select的默认值设为此记录,请问怎么设置?
我的select标签下已有option选项,页面显示时显示出与数据库中提取的值相等的那个选项。

普通的HTML select标签在显示的时候总是默认的显示第一个<option value="">中所指示的值。
只需要一个JS函数,就可以让它的默认值能够根据后台所传过来的参数不同而变化。
参考如下:
JSP文件:
<body onload="checkModel()">
<form action="<%=request.getContextPath()%>/secretboxInfoAction.do" method="post" name="ClientForm">
<tr>
<td class="td"><span class="font13b">型号:</span>
<select id="mod" style="width:115pt" name="model" value="" onkeydown="model_prompt_del()">
<option value="">------请选择------
<option value="转盘" >转盘
<option value="密码" >密码
<option value="金柜" >金柜
</select>
<span id="model_prompt" class="style1">*</span><html:errors property="model"/>
</tr>
</body>

<script type="text/javascript">
function checkModel() {
if("${secretboxInfo.model}"=="转盘"){
document.getElementById("mod").value="转盘";
} else if("${secretboxInfo.model}"=="密码"){
document.getElementById("mod").value="密码";
} else if("${secretboxInfo.model}"=="金柜"){
document.getElementById("mod").value="金柜";
} else{
document.getElementById("mod").value="";
}
}
</script>
温馨提示:内容为网友见解,仅供参考
第1个回答  2014-06-27
首先看下你要把第几条设置为默认;
<option> 的id规律化写成 id="option"+index;的形式;
第几条就是index;
$("#option"+index).css("selected":"selected");
第2个回答  2015-05-19
直接设置下拉框的值就可以了
$("#selectid").val("defaultValue");
第3个回答  推荐于2018-02-23
<select>
<option <% if(staff.getSection()==1){ out.print("selected=''")}%>>1</option>
<option <% if(staff.getSection()==2){ out.print("selected=''")}%>>2</option>
</select>本回答被网友采纳
相似回答