我想寫js,以便我的選擇下拉列表中獲取值和我的數組中的文本。 js對我來說是新的。 jsfiddle什麼是最好的方式來獲得選擇選項,從js數組值
我需要的值是數字和文本的文本引號:
var locations = [
[23, 'Main Room'],
[1, 'Main Lobby'],
[2, 'Training Room'],
[56, 'Main Office'],
[57, 'Lower Office'],
[9, 'Lower Lobby'],
[62, 'Conference Room'],
[22, 'Outdoor Patio'],
[63, 'Upper Lobby']
];
var select = document.getElementById("selectRoom");
for(var i = 0; i < locations.length; i++) {
var opt = locations[i];
var el = document.createElement("option");
el.textContent = opt; // I just want the text within quotes from
el.value = opt; // I just want the number from opt
select.appendChild(el);
}
或應我的數組是什麼樣子? locations = {「23」:「主廳」,「1」:「主大廳」};
快捷鍵:新選項(「key」,「value」); – dandavis