怎样用JS给option添加“选中”属性?
发布网友
发布时间:2022-04-20 22:52
我来回答
共1个回答
热心网友
时间:2023-10-21 09:33
[html] view plain copy print?
<html>
<head>
<script>
window.onload = function(){
var opts = document.getElementById("select");
var value = 2//这个值就是你获取的值;
if(value!=""){
for(var i=0;i<opts.options.length;i++){
if(value==opts.options[i].value){
opts.options[i].selected = 'selected';
alert(opts.options[i].value);
break;
}
}
}
}
</script>
</head>
<body>
<select id="select">
<option value="1">1231231</option>
<option value="2">12312312</option>
<option value="3">123123123</option>
</select>
</body>
</html>