我有问题可以从下面的代码更改选项的值,根据其他选择选择哪些更改,但是我想更改值选择ID = “numbList”,感谢....根据另一个选定的下拉列表更改下拉列表中的选项值
<!DOCTYPE html>
<html>
<body>
<select id="diffList" onchange="changeList()">
<option value="">-- Difficulty --</option>
<option value="1">Easy</option>
<option value="2">Medium</option>
<option value="3">Difficult</option>
</select>
<select id="numbList"></select>
<script>
window.difficulty = {};
window.difficulty['1'] = [1,2,3];
window.difficulty['2'] = [4,5,6];
window.difficulty['3'] = [7,8,9];
function changeList() {
var diffList = document.getElementById("diffList");
var numbRange = document.getElementById("numbList");
var selectDiff = diffList.options[diffList.selectedIndex].value;
while(numbRange.options.length)
{
numbRange.remove(0);
}
var diff = window.difficulty[selectDiff];
if(diff)
{
var i;
for(i = 0; i < diff.length; i++)
{
var difficulty = new Option(diff[i], i);
numbRange.options.add(difficulty);
}
}
}
</script>
</body>
</html>
我不明白什么是不工作。你想让numbList的值为1-9而不是0-2? – mollwe
'remove()'不会做你认为它正在做的事情。相反,您应该使用父元素中的'removeChild()'。 – grochmal
@mollwe ....是的,我只是想改变numbList上的值,因为他自动出现,(0,1,2)是它的值,我想改变 – eff