简体   繁体   English

Json级联下拉菜单

[英]Json cascading dropdown

I'm trying to set up a cascading dropdown using JSON data. 我正在尝试使用JSON数据设置级联下拉列表。 I have it working somewhat but need some assistance getting the 2nd tier to work as intended. 我有一些工作,但需要一些帮助才能使第二层按预期工作。 Currently it seems to be grabbing the number in the array. 当前,它似乎正在获取数组中的数字。

Ideally for my 2nd tier I want to show the text in the dropdown as the option text, and I'd like to use the id field in the json as the value of the option. 理想情况下,对于第二层,我想将下拉菜单中的文本显示为选项文本,并且我想使用json中的id字段作为选项的值。

var data = {
            "Crime":[
                {"id":"1","text":"Number of police"},
                { "id":"2","text":"Number of crimes"} 
                    ],
            "Health":[
                {"id":"3","text":"Number of doctors"},
                {"id":"4","text":"Number of hospital visits"},
                {"id":"5","text":"Number of nurses"} 
                    ],
                }

I have a jsfiddle showing what I have so far. 我有一个jsfiddle显示我到目前为止所拥有的。

Happy to use whatever combination of javascript/jquery works best. 很高兴使用javascript / jquery的任何组合效果最佳。

The way you have used for..in seems to be incorrect. 您用于for..in的方式似乎不正确。 The question variable will not contain the entire value if the pointed collection ( data[this.value] , in this case) is not a simple array. 如果有针对性的集合(在这种情况下为data[this.value] )不是简单数组,则question变量将不包含整个值。 Rather, it would contain the index of the first row, the second row and so on. 相反,它将包含第一行,第二行的索引,依此类推。 I request you to read this for a more in-depth understanding : 我要求您阅读此书以获得更深入的了解:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

This line here 这条线在这里

questionSel.options[questionSel.options.length] = new Option(question, question);

Must read this way 必须这样阅读

questionSel.options[questionSel.options.length] = new Option(
                                      data[this.value][question].text, 
                                      data[this.value][question].id);

Here's an updated fiddle after this change has been made: 这是进行此更改后的更新小提琴:

http://jsfiddle.net/tc1f3kup/2/ http://jsfiddle.net/tc1f3kup/2/

please try this 请尝试这个

 var data = {
        "Crime":[
            {"id":"1","text":"Number of police"},
            { "id":"2","text":"Number of crimes"} 
                ],

        "Health":[
            {"id":"3","text":"Number of doctors"},
            {"id":"4","text":"Number of hospital visits"},
            {"id":"5","text":"Number of nurses"} 

                ],

            }
 window.onload = function () {
        var themeSel = document.getElementById("theme"),
        questionSel = document.getElementById("questions");
        for (var theme in data) {
            themeSel.options[themeSel.options.length] = new Option(theme, theme);
        }
        themeSel.onchange = function () {
            questionSel.length = 1; // remove all options bar first
            if (this.selectedIndex < 1) return; // done                 
                for(var i=0;i<data[this.value].length;i++)
                {                        
                    questionSel.options[questionSel.options.length] = new Option(data[this.value][i].text, data[this.value][i].id);
                }



        }
}

working fiddle http://jsfiddle.net/tc1f3kup/3/ 工作小提琴http://jsfiddle.net/tc1f3kup/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM