gpt4 book ai didi

javascript - 如何在javascript中添加选择

转载 作者:行者123 更新时间:2023-12-03 07:18:57 25 4
gpt4 key购买 nike

我正在尝试在选择标签中添加选项,但它不起作用。

container.appendChild(document.createTextNode("location"));
var select = document.createElement("select");
var option1 = document.createElement("option");
option1.text = "mumbai";
option1.value = "mumbai";
container.appendChild(option1);
container.appendChild(select);

最佳答案

您正在将选项添加到容器而不是选择。因此,将 container.appendChild(option1); 更改为 select.appendChild(option1);:

var container = document.getElementById('elem')
container.appendChild(document.createTextNode("location"));
var select = document.createElement("select");
container.appendChild(select);
var counter = 0;
document.getElementById("btn").addEventListener("click", function() {
counter++;
var option1 = document.createElement("option");
option1.text = "mumbai" + counter;
option1.value = "mumbai" + counter;
select.appendChild(option1);
});
<div id='elem'>

</div>
<button name='btn' id='btn'>Add</button>

关于javascript - 如何在javascript中添加选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41197914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com