【问题标题】:How can I generate a list of links based on dropdown menu choices?如何根据下拉菜单选项生成链接列表?
【发布时间】:2014-04-24 21:22:28
【问题描述】:

所以我有一堆不同国家/地区的信息,我正在尝试将其排序如下:

选择国家的下拉菜单 -> 选择信息类型的下拉菜单 -> 这是该信息的链接

我对 javascript 不太了解,但我发现这个解决方案允许我根据从第一个下拉列表中选择的选项更改第二个下拉列表的内容:

<script type="text/javascript">
    function configureDropDownLists(ddl1, ddl2) {
        var albania = new Array('History', 'Legal Guides');
        var andorra = new Array('Country Overview', 'Demographics', 'Legal Guides');


        switch (ddl1.value) {
            case 'Albania':
                document.getElementById(ddl2).options.length = 0;
                for (i = 0; i < albania.length; i++) {
                    createOption(document.getElementById(ddl2), albania[i], albania[i]);
                }
                break;
            case 'Andorra':
                document.getElementById(ddl2).options.length = 0;
                for (i = 0; i < andorra.length; i++) {
                    createOption(document.getElementById(ddl2), andorra[i], andorra[i]);
                }
                break;
        }

    }

    function createOption(ddl, text, value) {
        var opt = document.createElement('option');
        opt.value = value;
        opt.text = text;
        ddl.options.add(opt);
    }
</script>

然后是下拉框,在 HTML 中:

<select id="ddl" onchange="configureDropDownLists(this,'ddl2')">
<option value=""></option>
<option value="Albania">Albania</option>
<option value="Andorra">Andorra</option>
</select>

<select id="ddl2">
</select>

所以现在我想知道如何采用第二个选择并使用它来生成链接列表供某人选择 - 例如,在新的文本段落或其他内容中。

第一次在这里提问,如有困惑,抱歉。

【问题讨论】:

  • 你能给出一些有代表性的数据来​​说明你的问题吗?如果第一个&lt;select&gt; 元素有两个选项ab,那么第二个&lt;select&gt; 应该提供哪些选项?第二个&lt;select&gt; 中选择的选项将如何影响要显示的链接?
  • 第一个选择元素将有大约四十个选项——所有不同的国家。然后,第二个选择元素将显示最多四个选项——历史、法律指南、国家概览和人口统计(我没有某些国家/地区的一些信息,因此根据选择的国家/地区可能会更少比第二个选择元素中的最多四个选项)。
  • 一旦选择了第二个选择元素,我想创建一个指向该信息的链接列表(每个国家/地区的每个类别我可能有多个链接)。所以首先我会选择安道尔,然后第二个选择将填写我所拥有的关于安道尔的四类信息(历史、法律指南、国家概况和人口统计)。然后,如果我选择人口统计,我选择的关于安道尔人口统计的文章/网站的链接列表将在同一页面上的
      元素中生成。

标签: javascript html arrays drop-down-menu


【解决方案1】:

首先添加这个链接列表可以去的地方。 我会把它放在一个无序列表中(&lt;ul&gt;&lt;/ul&gt;)。但是你也可以把它放在一个段落或一个 div 中。

我假设您了解对象和 for / in 循环。 如果没有,这应该可以帮助您获得它: https://javascriptweblog.wordpress.com/2011/01/04/exploring-javascript-for-in-loops/

这是我为您编写的代码。我一直在评论它。 只是问是否有不清楚的地方:) 阿尔巴尼亚 安道尔

    <select id="ddl2" onchange="configureDropDownLists('ddl2')">
    </select>

    <ul id='linkList'></ul>

    <script type="text/javascript">
    function configureDropDownLists(ddlBeingChanged) {
        var ddl = document.getElementById('ddlBeingChanged');
        var ddl1ChosenValue=document.getElementById('ddl1').value;


        var linkLists = {
            albania: {
                "history": ['http://albania.example.com/history', 'http://albania.example.com/historyTwo'],
                "legal guides": ['http://albania.example.com/guide', 'http://albania.example.com/guideTwo'],
            },

            andorra: {
                "country overview": ['http://andorra.example.com/country', 'http://andorra.example.com/overview'],
                "demographics": ['http://andorra.example.com/demographics', 'http://andorra.example.com/demographicsTwo'],
                "legal guides": ['http://andorra.example.com/guide', 'http://andorra.example.com/guideTwo'],
            }
        };

        if (ddlBeingChanged == "ddl1") {
            console.log(ddl1ChosenValue);
            for (var ddl2 in linkLists[ddl1ChosenValue]){
                console.log(ddl2);
                // Here the ddl2 variable will contain the first level of the object 'linkLists'. I.E. the country names.
                createOption(document.getElementById('ddl2'), ddl2, ddl2);    
            }
        } else if (ddlBeingChanged == "ddl2") {
            var ddl2ChosenValue=document.getElementById('ddl2').value;


            var linkArray=linkLists[ddl1ChosenValue][ddl2ChosenValue];
            // The linkArray:
            // Let's say someone chose andorra and demographics
            // then linkLists[ddl1ChosenValue][ddl2ChosenValue] would be equivalent to linkLists.andorra.demographics

            var linkListHTML="";
            for (var i in linkArray){
                var URL=linkArray[i];
                linkListHTML+="<li><a href='"+URL+"'>"+URL+"</a></li>";
            }
            document.getElementById('linkList').innerHTML=linkListHTML;

        }


    }

    function createOption(ddl, text, value) {
        var opt = document.createElement('option');
        opt.value = value;
        opt.text = text;
        ddl.options.add(opt);
    }
    </script>

编辑:修复代码错误

【讨论】:

  • 感谢您的回复。我尝试了您的代码,但似乎没有填充辅助下拉列表。恐怕我对 javascript 真的很陌生。不过,我会阅读您链接到的文章。
  • 我太失败了。自己没有运行代码。我会运行它并更新它:)
  • 现在可以了。仍然可能存在区分大小写的错误。仍然尝试浏览代码并了解正在发生的事情。另外我建议学习jQuery。它是一个 javascript 库,可以更轻松地执行此类操作以及更多操作。
猜你喜欢
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多