JS 控制select选中项,代码如下:
<html><script type="text/javascript">
var selectedValue = '<%= request.getAttribute("line")%>'
function changeSelected(){
jsSelectItemByValue(document.getElementById("mySelect"),selectedValue)
}
function jsSelectItemByValue(objSelect,objItemText) {
for(var i=0i<objSelect.options.lengthi++) {
if(objSelect.options[i].value == objItemText) {
objSelect.options[i].selected = true
break
}
}
}
</script>
<body onload="changeSelected()">
<select id="mySelect" name="mySelect">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</body>
</html>
再加个判断,如果res_id==003 就给option一个属性 selected="selected"。这样就默认选中的。输出出来应该是这样的
<option value="003" selected="selected">补考</option><SELECT>标签用multiple属性来控制多选或单选,默认为单选,即没有multiple属性,所以设置单选则使用JS的removeAttribute函数移除multiple属性即可,代码如下所示:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>
<head>
<title>Select 单选</title>
</head>
<body>
<select name="s2" size="6" style="width:100px" id="s2" multiple="multiple">
<option>选项1</option>
<option>选项2</option>
<option>选项3</option>
<option>选项4</option>
<option>选项5</option>
</select>
<script type="text/javascript">
var s2=document.getElementById("s2")
s2.removeAttribute("multiple")
</script>
</body>
</html>
效果如下: