javascript - 如何从 JSON 创建下拉菜单

标签 javascript html css json

是否有人创建或完成了编程以创建基于 JSON 结构的菜单和下拉菜单。

下面是我的 JSON 菜单结构:

Screenshot

[
        {"root" : "Input", 
         "subs" : [
                  {"sub_name": "CSV Reader","type": "csv",
                     "subs_level2": [{"sub_name": "Reader 1","type": "csv"},
                                     {"sub_name": "Reader 2","type": "csv"}
                                     ]
                  },
                  {"sub_name": "DB Connect","type": "db"},
                  {"sub_name": "Sample Data","type": "sample data"}
                  ]
        },
        {"root" : "Output", 
         "subs" : [
                  {"sub_name": "Output 1","type": "output1"},
                  {"sub_name": "Output 2","type": "output2"},
                  {"sub_name": "Output 3","type": "output3"}
                  ]
        }
     ]

这是我最初的 javascript:

// setup the main div
//<div id="canvas_dock"></div>
//$('#canvas_dock').append("<div class='node_dropdown'>
//<span>Input (Root)</span>
//<div class='node_dropdown-content'>
//<div id="">CSV Reader</div>
//<div id="">DB Connect</div>
//<div id="">Sample Data</div>
//</div>
//</div>");

//here is the loop
var key = "root", idx = 0;
for(key in nodeTypes){
    if(nodeTypes[idx].root != undefined && nodeTypes[idx].root == root_menu){
      for(var sub in nodeTypes[idx].subs){
        $('#node_dropdown-content').append(nodeTypes[idx].subs[sub].sub_name);
      }
    }
    idx = idx + 1;
}

最佳答案

您好,请使用 Javascript 检查一下

 var JSON =[
    {
        "root": "Input",
        "subs": [
            {
                "sub_name": "CSV Reader",
                "type": "csv",
                "subs_level2": [
                    {
                        "sub_name": "Reader 1",
                        "type": "csv"
                    },
                    {
                        "sub_name": "Reader 2",
                        "type": "csv"
                    }
                ]
            },
            {
                "sub_name": "DB Connect",
                "type": "db"
            },
            {
                "sub_name": "Sample Data",
                "type": "sample data"
            }
        ]
    },
    {
        "root": "Output",
        "subs": [
            {
                "sub_name": "Output 1",
                "type": "output1"
            },
            {
                "sub_name": "Output 2",
                "type": "output2"
            },
            {
                "sub_name": "Output 3",
                "type": "output3"
            }
        ]
    }
]


    var select = document.getElementById("selector");
    for (var i = 0; i < JSON[0].subs.length; i++) {
        var option = document.createElement("option");
        option.value = i;
        option.textContent = JSON[0].subs[i].sub_name;
        select.appendChild(option);
    };

引用https://plnkr.co/edit/FZNFuu1G3KAvPRvPGtqV?p=preview

关于javascript - 如何从 JSON 创建下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38177810/

相关文章:

javascript - 通过套接字传输时数组数据被复制(Socket.io)

javascript - 翻译单页应用程序

html - 有没有工具可以获取 css 类并使它们内联?

CSS @Media Print - 内联元素的水平间距

javascript - 使用 z-index 解决方法的 Z-index 绝对定位元素位于父级下方?

JavaScript:从对象数组中获取唯一值及其计数?

javascript - 自动将 HTML 插入页面上某些点的代码?

html - 水平对齐在 Firefox 和 IE 中不起作用

css - 如何更改推特 Bootstrap 背景颜色

jquery - 覆盖不覆盖导航栏