怎么在js中获得select标签被选中的值
2个回答
展开全部
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=0;i<objSelect.options.length;i++) {
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>
<html>
<script type="text/javascript">
var selectedValue = '<%= request.getAttribute("line")%>';
function changeSelected(){
jsSelectItemByValue(document.getElementById("mySelect"),selectedValue);
}
function jsSelectItemByValue(objSelect,objItemText) {
for(var i=0;i<objSelect.options.length;i++) {
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>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询