[英]How to populate drop down using two dimension array in javascript
I'm trying to create two dimensional array and populate a dropdown using it. 我正在尝试创建二维数组并使用它填充下拉列表。
But I'm getting Uncaught SyntaxError: Unexpected string error. 但是我收到了Uncaught SyntaxError:意外的字符串错误。
var cuisines = ["001","Australian"],["002","American"];
var sel = document.getElementById('CuisineList');
for(var i = 0; i < cuisines.length; i++) {
var opt = document.createElement('option');
opt.innerHTML = cuisines[i][1];
opt.value = cuisines[i][0];
sel.appendChild(opt);
}
<select id="CuisineList"></select>
Please help me. 请帮我。 Thanks. 谢谢。
You need outer brackets when defining your two dimensional array. 定义二维数组时,需要使用外括号。
var cuisines = [["001","Australian"],["002","American"]];
Also, looks like you're using the letter "o" instead of the number 0 for one of your indexes. 另外,看起来您正在使用字母“ o”而不是其中一个索引的数字0。
I did not investigate your code deeply but you have error: 我没有深入调查您的代码,但是您有错误:
opt.value = cuisines[i][o];
should be 应该
opt.value = cuisines[i][0];
you are using "o" letter instead of 0 (zero) 您使用的是“ o”字母而不是0(零)
Change var cuisines = ["001","Australian"],["002","American"];
更改var cuisines = ["001","Australian"],["002","American"];
to var cuisines =[ ["001","Australian"],["002","American"]];
var cuisines =[ ["001","Australian"],["002","American"]];
so you actually have a two dimensional array, and change your [o]
to [0]
. 所以实际上您有一个二维数组,并将[o]
更改为[0]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.