<select id="comboBox">
<option id="1">One</option>
<option id="2">Two</option>
<option id="3">Three</option>
<option id="4">Four</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我想选择ID为3的选项.有没有办法在没有循环的情况下怎么做?我期待这样的事情
$("#comboBox").val("3");
Run Code Online (Sandbox Code Playgroud)
但是使用ID而不是值.(所以我的意思是作为comboBox的成员访问该选项,而不是通过选择器之类document.getElementById('3');)
Zla*_*tev 12
<script type="text/javascript">
document.getElementById("3").selected=true;
</script>
Run Code Online (Sandbox Code Playgroud)
要么
<script type="text/javascript">
document.getElementById("comboBox").options[2].selected=true;
</script>
Run Code Online (Sandbox Code Playgroud)
要么
<script type="text/javascript">
document.getElementById("comboBox").selectedIndex=2;
</script>
Run Code Online (Sandbox Code Playgroud)
要么
<script type="text/javascript">
document.getElementById("comboBox").options.namedItem("3").selected=true;
</script>
Run Code Online (Sandbox Code Playgroud)
对于最后一个,请参阅https://developer.mozilla.org/en/docs/Web/API/HTMLSelectElement#namedItem()和 http://www.w3schools.com/jsref/coll_select_options.asp
您可以跳过对较短选项的引用 document.getElementById("comboBox").namedItem("3").selected=true;