简体   繁体   English

如何在下拉选项中显示数组列表

[英]How to show array list in dropdown option

after many types trying and search on google now i have no option to ask here..在谷歌上尝试和搜索了许多类型之后,我现在没有选择在这里问..

my question is i want to show data in select option dropdown menu.. the date is json array list..我的问题是我想在 select 选项下拉菜单中显示数据.. 日期是 json 数组列表..

here is my code这是我的代码

<select name="" id="state_list">
     <option>select state</option>
</select>



<script>      
var url = "#";

    function getData() {
      fetch(url)
        .then((data) => {
          return data.json();
        })
        .then((covidData) => {

          var dropdown = document.getElementById("state_list");
          var stateList = covidData.data.regional;

     for (i = 0; i < stateList.lenght; i++) {
         dropdown[dropdown.lenght] = new Option(stateList[i],stateList[i]); 
    }
     })
 </script>

You could do something like below to populate your stateList to your drop-down.您可以执行以下操作来将您的stateList填充到您的下拉列表中。

var select = document.getElementById("state_list"); 
for(var i = 0; i < stateList.length; i++) {
    var opt = stateList[i];
    var el = document.createElement("option");
    el.textContent = opt;
    el.value = opt;
    select.appendChild(el);
}​

A working fiddle:一个工作小提琴:

http://jsfiddle.net/shyntz0o/1/ http://jsfiddle.net/shyntz0o/1/

You have a typo in lenght and did not close the function correctly (missing the final } ).您的lenght有误,并且没有正确关闭 function (缺少最后的} )。 Also your option appending logic does not work out (again because of the typo lenght ).此外,您的选项附加逻辑也不起作用(再次因为错字lenght )。

 window.onload = function(){ var url = "https://api.rootnet.in/covid19-in/stats/latest"; function getData(){ fetch(url).then((data) => { return data.json(); }).then((covidData) => { var dropdown = document.getElementById("state_list"); var stateList = covidData.data.regional; for(var i=0; i<stateList.length; i++){ dropdown.appendChild(new Option(stateList[i].loc, stateList[i].loc)) } }) }; getData() }
 <select name="" id="state_list"> <option>select state</option> </select>

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

相关问题 如何在表中该行的其他可用下拉列表中显示从下拉列表中取消选择的选项-jQuery - how to show the deselected option from the dropdown list in the other available dropdown list of that row in a table - jquery 在引导下拉列表菜单中显示选定的选项 - Show selected option in bootstrap dropdown list menu 下拉列表选项值和名称与数组javascript - Dropdown list option value and name with array javascript 如何显示和隐藏一个<option value="per a dropdown selection?">每个下拉选择?</option> - How to show and hide an <option> per a dropdown selection? 如何使用 mongoDB 在下拉菜单中显示所选选项? - How to show the selected option in a dropdown menu, with the mongoDB? 如何根据下拉列表中当前选择的选项显示特定数据? - how can i show specific data based on currently selected option in a dropdown list? 如何基于另一个下拉列表中的选择来选择下拉列表中的选项 - How to select an option in a dropdown list based on a selection in another dropdown list 在按钮中显示下拉选项 - Show dropdown option in button 根据下拉列表中的选定选项显示数据库中的数据 - Show data from database depending on selected option in a dropdown list 从下拉列表中选择选项时,Javascript显示隐藏的div - Javascript to show hidden div when option is selected from dropdown list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM