【问题标题】:Open the html selected list with a button click通过单击按钮打开 html 选定列表
【发布时间】:2012-04-26 06:27:22
【问题描述】:

我在网页中创建了一个选项列表,我想用<div> 创建的按钮打开选择列表,当从列表中选择一个选项时,所选值也出现在 div 上。

所以在这里我希望 javascript 属性打开按钮单击类似这样的列表

document.getElementById('selectedlist').open.

任何机构对此有建议吗?

【问题讨论】:

  • 这里我们可以查看kriangkrai答案的demo

标签: javascript html


【解决方案1】:

看看http://code.google.com/p/expandselect/。我想这就是你想要的。 然后就可以这样打开选择了。

ExpandSelect("myselect_id")

【讨论】:

  • 看看这将如何在移动浏览器中工作(43% 的客户端),而许多或所有移动 webkit 版本只需专注于选择即可满足用户最初的需求。
  • @HarishKumar 你能展示你的代码吗?在您的页面中包含ExpandSelect.js 后,您可以像这样在单击按钮时打开选择。 <input type="button" value="open select" onclick="ExpandSelect('selectedlist');" />
  • 这里我使用的代码
    All Stores ↡
    类别是 id选择列表。
  • @Kriangkrai Bumroungruksa 哦,是的,抱歉,它成功了,我当时更改了 ID。非常感谢
  • @HarishKumar 不客气。如果您认为这种方法可以解决您的问题,请按复选标记将其标记为已接受的答案。您也可以考虑接受您的其他问题。
【解决方案2】:

这是在单击按钮时打开(切换)选择框的简单 Javascript 代码。

<script type="text/javascript">
    function expandSelect(id){
    var select_flag = document.getElementById('select_flag').value;
        if(select_flag==1){
            var select_box = document.getElementById(id);
            select_box.size = 1;
            document.getElementById('select_flag').value = 0;
        }else{
            var select_box = document.getElementById(id);
            select_box.size = select_box.options.length; 
            document.getElementById('select_flag').value = 1;
        }
   }
</script>

<button onclick="expandSelect('select_box')"> Toggle Select Box </button>


<div id="container">
    <select name="select_box" id="select_box" >
        <option value="">Select</option>
        <option value="">option1</option>
        <option value="">option2</option>
        <option value="">option3</option>
        <option value="">option4</option>
        <option value="">option5</option>
        <option value="">option6</option>
    </select>
    <input type="hidden" name="select_flag" id="select_flag" value="0">
</div>

【讨论】:

    猜你喜欢
    • 2014-11-06
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多