JavaScript - 强制选择元素下拉?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6210800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:52:02  来源:igfitidea点击:

JavaScript - Force select element to dropdown?

javascriptselectdrop-down-menu

提问by dpp

How can I force the select element to dropdown using Javascript? Is it possible?

如何使用 Javascript 强制选择元素下拉?是否可以?

<select id="MiamiPlayers">
    <option value="0">Bosh</option>
    <option value="1">Wade</option>
    <option value="2">LeBron</option>
</select>

<input type="button" onclick="Show()" />

<script type="text/javascript">
    function Show()
    {
        //force the select to dropdown...
    }
</script>

Thanks in advance!

提前致谢!

采纳答案by Dinash

var select = document.getElementById(id);
select.size = select.options.length;

where id is the id of the your select box...

其中 id 是您选择框的 id ...

回答by Petah

You can't open the drop down, but you can do something like this:

您无法打开下拉菜单,但您可以执行以下操作:

document.getElementById('MiamiPlayers').setAttribute('size', 3);

See http://jsfiddle.net/MdCBB/

http://jsfiddle.net/MdCBB/