简体   繁体   English

使用Vanilla JS和JSON文件填充选择元素

[英]Populate select element using Vanilla JS and JSON file

I'm trying to populate a select element using data from an external json file. 我正在尝试使用来自外部json文件的数据填充select元素。

var dropDown = document.getElementById('dropdown');

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    // success
    data = JSON.parse(request.responseText);
    console.log(data);

    for (var i = 0, len = data.length; i < len; i++) {
        alert('FOO');
        var data = data[i];
        dropDown.innerHTML(option.name);
    }
};

JSON JSON

{
    "TopLevel": {
        "Second": [
            "data",
            "data",
            "data",
            "data"
        ],
        "Second": [
            {
                "ThirdLabel": "data",
                "ThirdID": "data"
            },
            {
                "ThirdLabel": "data",
                "ThirdID": "data"
            },
            {
                "ThirdLabel": "data",
                "ThirdID": "data"
            }
        ]
    },
    "TopLevel": {
        "Second": [
            "data",
            "data",
            "data",
            "data"
        ],
        "Second": [
            {
                "ThirdLabel": "data",
                "ThirdID": "data"
            },
            {
                "ThirdLabel": "data",
                "ThirdID": "data"
            },
            {
                "ThirdLabel": "data",
                "ThirdID": "data"
            }
        ]
    }
}

I've can successfully return my data using the above but for some reason I cant get the loop to run and im unsure what I'm doing wrong. 我已经可以使用上述方法成功返回数据,但是由于某些原因,我无法使循环运行并且不确定自己在做什么错。

There's no console errors and my alert doesn't fire... 没有控制台错误,我的警报也不会触发...

The best way to add option to a select is using document.createElement("option") according to W3schools Select add() Method . 向选项添加选项的最佳方法是根据W3schools Select add()方法使用document.createElement("option")

 var json = { "data": ["orange", "banana", "apple", "pear"] }; var dropdown = document.getElementById("dropdown"); for (var i = 0; i < json.data.length; i++) { var option = document.createElement("option"); option.text = json.data[i]; option.value = json.data[i]; dropdown.add(option); } 
 <select id="dropdown"></select> 

You need to write code in this way. 您需要以这种方式编写代码。

The data is returning object {} therefore the length of the data is undefined and loop is not running. 数据正在返回object {}因此数据的长度undefined并且循环未运行。

You need to select particular data which will return array. 您需要选择将返回数组的特定数据。

Ex: data.TopLevel.Second 例如: data.TopLevel.Second

 var data = { "TopLevel": { "Second": [ "data", "data", "data", "data" ], "Second": [{ "ThirdLabel": "data", "ThirdID": "data" }, { "ThirdLabel": "data", "ThirdID": "data" }, { "ThirdLabel": "data", "ThirdID": "data" } ] }, "TopLevel": { "Second": [ "data", "data", "data", "data" ], "Second": [{ "ThirdLabel": "data", "ThirdID": "data" }, { "ThirdLabel": "data", "ThirdID": "data" }, { "ThirdLabel": "data", "ThirdID": "data" } ] } } var dropDown = document.getElementById('dropdown'); debugger; var options = ""; for (var i = 0, len = data.TopLevel.Second.length; i < len; i++) { var item = data.TopLevel.Second[i]; options += "<option> " + item.ThirdID + "</option>" } document.getElementById("dropdown").innerHTML = options; 
 <select name="" id="dropdown"></select> 

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

相关问题 Populate a Select Dropdown Lists using JSON, from a given extarnal json object with async/await methods and try and catch statements in vanilla js - Populate a Select Dropdown Lists using JSON, from a given extarnal json object with async/await methods and try and catch statements in vanilla js 如何使用 vanilla javascript 用数组的内容填充 html select 元素? - How can I populate a html select element with the contents of an array using vanilla javascript? 使用 vanilla JS 之后使用 API 中的数据填充 select(下拉)。 - Populate select (drop down) with data from API after .map using vanilla JS 根据JSON填充选择元素 - populate select element based on json 首先如何通过Ajax调用将数据填充到select元素中 - how to Populate datas into select element by Ajax call using js at first 如何使用 JS fetch() 使用 JSON 值列表填充 select 下拉列表? - How to populate a select dropdown with a list of JSON values using JS fetch()? 在 vanilla JS 中使用 select 进行同位素过滤是有问题的 - Isotope Filtering using select in vanilla JS is buggy 使用 Vanilla JS 重新排序 select 选项列表 - Reorder select option list using Vanilla JS 如何使用vanilla JS减去元素的高度? - How to subtract the height of element using vanilla JS? 使用香草JS在html表格上显示JSON - display JSON on an html table using vanilla JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM