[英]Update selected value in dropdown list depending on other lists selected values
[英]How do you remove values in other dropdown lists when a value has been selected, then restore the values if they become available again?
假設我在選擇HTML標記中列出了數字1-4和“無”選項。 我想將這些標簽中的四個放在頁面上,用戶可以在其中下拉菜單並為每個標簽選擇一個數字,但不能兩次選擇相同的數字。 如果他們將選擇框之一切換回無,則該數字應再次可用。 我在另一個線程(如下所示)上找到了一些代碼,這些代碼將阻止您選擇下一個框中的值,但是該值將被永久刪除。 我希望保留選項,最好按它們開始的順序保留。
<script>
document.getElementById("select1").addEventListener("change",
function(select)
{
if (this.value != "")
{
var select2 = document.getElementById("select2");
for (var i = 0; i < select2.options.length; i++)
{
if (this.value == select2.options[i].value)
{
select2.options[i] = null;
break;
}
}
}
}, false);
</script>
您可以在數組中創建相同的列表。 在列表中刪除時,您的數組仍包含值。 再次需要時,只需將數組中的選擇值放回選項中即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.