我有問題可以從下面的代碼更改選項的值,根據其他選擇選擇哪些更改,但是我想更改值選擇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