Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果选择元素已被选中,我如何访问它?
var select = document.getElementById("select"); if(select.selected == true) doesnt work any idea?
DOM 树类似于
Select |-option |-option `-option
不是可以选择的选择节点。因此,您需要获取选择,浏览孩子,并测试孩子是否被选中,直到找到一个。
document.getElementById("mySelect").value
或者
var selObj = document.getElementById("mySelect"); selObj.options[selObj.selectedIndex].value;