[英]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.