我正在尝试使用 javascript 将一些项目从文本框添加到列表框。但问题是,一旦我添加项目,它就会在列表框中显示一秒钟,然后被删除。有什么解决办法吗?
我正在将 TextBox4 中的项目添加到 ListBox1
function AddToList() {
// Create an Option object
var opt = document.createElement("option");
// Add an Option object to List Box
document.getElementById("ListBox1").options.add(opt);
opt.text = document.getElementById("TextBox4").value;
opt.value = document.getElementById("TextBox4").value;
document.getElementById("ListBox1").options.add(opt);
}
Run Code Online (Sandbox Code Playgroud)
document.getElementById("ListBox1").options.add(opt); // remove this line
opt.text = document.getElementById("TextBox4").value;
opt.value = document.getElementById("TextBox4").value;
document.getElementById("ListBox1").options.add(opt);
Run Code Online (Sandbox Code Playgroud)
当你附加一个<option>没有valueor 的text。