重置一个选择元素 [英] reset a select element
本文介绍了重置一个选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何选择第一个选项?我试过这个,但是没有工作:
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;
希望对您有用。
这篇关于重置一个选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文