gpt4 book ai didi

javascript - 在 JavaScript 中生成下拉列表(未知错误)

转载 作者:行者123 更新时间:2023-12-03 07:20:52 24 4
gpt4 key购买 nike

每次用户更改 List1 (selQueryType) 中的选择时,我都想重新生成 List2 (selSortt)。在其原始状态下,List2空白。这两个列表都是更大的 HTML 输入文档的一部分。 List1 有 4 个选项,因此最终会有 4 个 if - 实际上是一个 switch列表 2 是从预定义数组 (listvalues) 生成的,而不是从 List1 生成的(请参阅代码)。

函数 QueryTypeChg()onchange List1 触发 - 我检查了警报,它被触发。当尝试生成 List2 的新选项时,问题似乎出在 for 循环中。 未向 List2 添加任何选项 - 它保持空白,与原来一样。

function QueryTypeChg()
{
var selIndex = document.getElementById("selQueryType").selectedIndex;

var objSel = document.getElementById("selSortt");
var i = 0;

while (objSel.length > 0)
{
objSel.remove(0);
}

if (selIndex == 0)
{
var listvalues = ["Species", "Region, area", "Anthesis", "Vulnerability"];

for (i = 0; i < options.length; i++)
{
var option = document.createElement("option");
option.text = listvalues[i];
option.value = i;
// alert("option.text is " + option.text);
objSel.add(option);
}​
}


}

最佳答案

因为你是removing the element通过做

while (objSel.length > 0)
{
objSel.remove(0);
}

如果您想清除选择,只需将此 while 循环替换为

objSel.innerHTML = "";

完整的代码应该是

function QueryTypeChg()
{
document.getElementById("selSortt").innerHTML = document.getElementById("selQueryType").innerHTML
document.getElementById("selSortt").selectedIndex = document.getElementById("selQueryType").selectedIndex;
}

关于javascript - 在 JavaScript 中生成下拉列表(未知错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36218442/

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