gpt4 book ai didi

JavaScript - 检查多个 SELECT 是否有重复选项

转载 作者:行者123 更新时间:2023-12-02 18:17:24 24 4
gpt4 key购买 nike

此问题基于 THIS QUESTION

当选择其中一个选择框中的一个选项时,我希望重新填充其余的选项,而不需要上述选项,但是是否有一种简单的方法可以循环遍历所有这些选择项,以确保相同的选项没有'没有被选中两次?

谢谢。

Person Number 1
<select name="person1">
<option value="null">Please Select an option</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

Person Number 2
<select name="person2">
<option value="null">Please Select an option</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

Person Number 3
<select name="person3">
<option value="null">Please Select an option</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

基本概述:JavaScript 循环以确保没有任何选项被选择两次?

最佳答案

<!DOCTYPE html>
<html>
<head>
<script>
function doAction(el) {

for (var i = 0; i < document.getElementById('person2').length; i++) {
var v = (i != el.selectedIndex ? '' : 'disabled');

document.getElementById('person2')[i].disabled = v;
if (document.getElementById('person2').selectedIndex == el.selectedIndex)
document.getElementById('person2').selectedIndex = 0;

document.getElementById('person3')[i].disabled = v;
if (document.getElementById('person3').selectedIndex == el.selectedIndex)
document.getElementById('person3').selectedIndex = 0;
}
}
</script>
</head>
<body>

Person Number 1
<select id="person1" onchange="doAction(this)" >
<option value="null">Please Select an option</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br/>
Person Number 2
<select id="person2">
<option value="null">Please Select an option</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br/>
Person Number 3
<select id="person3">
<option value="null">Please Select an option</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

</body>
</html>

关于JavaScript - 检查多个 SELECT 是否有重复选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19163945/

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