隐藏时重置下拉菜单

时间:2015-08-19 17:40:45

标签: javascript drop-down-menu show-hide

我有一个表单,它使用一个由单选按钮切换的下拉菜单。但是,如果隐藏菜单,我会尝试清除菜单选项。我使用JavaScript来显示/隐藏下拉列表,但我似乎无法在隐藏时清除下拉菜单。

这是我到目前为止的JavaScript,任何见解都会受到赞赏:

<script type="text/javascript">
                        if(document.readyState === "complete")
                        {
                            if (document.getElementById('yesCheck').checked) {
                                document.getElementById('DropDownList').style.visibility = 'visible';
                            }
                        }
                        function yesnoCheck() {
                            if (document.getElementById('yesCheck').checked) {
                                document.getElementById('DropDownList').style.visibility = 'visible';
                            }
                            else {
                                document.getElementById('DropDownList').style.visibility = 'hidden';
                                var elements = document.getElementById("DropDown").options;

                                for(var i = 0; i < elements.length; i++){
                                  elements[i].selected = null;
                                }
                            }
                        }   

1 个答案:

答案 0 :(得分:0)

试试这个

document.getElementById("DropDownList").selectedIndex = "0";

http://www.w3schools.com/jsref/prop_select_selectedindex.asp