[英]fill a <select> with a function Javascript
I created a function which contains a string that I want to add to a in HTML. 我创建了一个函数,该函数包含要添加到HTML中的字符串。 But when I test it, it won't show me the options declared in my table. 但是,当我对其进行测试时,它不会向我显示表中声明的选项。
function test(){
// table already defined
var choices = "<option selected>Select...</option>";
for (var i=0; i<tableOptions.length; i++) {
choices += "<option>" + tableOptions[i][0] +"</option>";
}
document.getElementById("theOptions").innerHTML = choices;
}
and in my HTML I have 在我的HTML中
<select id="theOptions"></select>
What am I doing wrong? 我究竟做错了什么?
By the way, my test() is automatically loaded after my page is displayed. 顺便说一句,我的test()在显示我的页面后自动加载。
<body onload="test()">
See How to populate the options of a select element in javascript for detail on creating and appending options to an existing <select>
element. 有关创建选项并将其附加到现有的<select>
元素的详细信息,请参见如何在javascript中填充select元素的选项。
Using that method, this seems closest to what you're getting at: 使用该方法,这似乎与您所获得的最接近:
var select = document.getElementById("theOptions");
opt = document.createElement("option");
opt.innerHTML = "Select...";
select.appendChild(opt);
for(var i = 0; i < tableOptions.length; i++)
{
var opt = document.createElement("option");
opt.innerHTML = tableOptions[i][0];
select.appendChild(opt);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.