重置一个选择元素
问题描述:
如何选择第一个选项?我试过这个,但是没有工作:
how to make the first option selected? I tried this but doesnt work:
selectElement.options[0].selected=true;
它给我这个错误:
Uncaught TypeError: Cannot read property '0' of undefined
更新
已解决!通过正确定义selectElement。
update
solved! by defining selectElement correctly.
答
尝试这个(参见演示)
HTML
<select id="selectElement">
<option>First</option>
<option selected>Second</option>
</select>
JavaScript
JavaScript
selectElement.options[0].selected="selected";
或者你可以做 selectElement.options [0] .selected = true;
希望对您有用。