gpt4 book ai didi

javascript - 有没有一种简单的方法来进行 HTML 关系选择?

转载 作者:可可西里 更新时间:2023-11-01 13:42:35 24 4
gpt4 key购买 nike

有4个select标签,每个select有4个选项,分别是1,2,3,4。现在,如果我随机选择一个标签,其他标签选项应该减少。

例如:

  • tag1 tag2 tag3 tag4 开头都是空的;

    tag1 是用选项 2 选择的;

    tag2, 3, 4:option: 1, 3, 4(option 2被移除);

    tag2是用option 1选择的;

    tag3, 4: option: 3, 4 (option 1 被移除);

    tag3是用option 3选择的;

    tag4:选项4(选项3被移除);

    tag4 被选中。

我是用 javascript 做的,但问题是……

在选择了所有标签后,我想更改其中一个标签(其他标签应该会自动相应地更改)。

有更好的方法吗?

我的代码:

           function removeOption()
{
var p1 = document.getElementById("player1");
var p2 = document.getElementById("player2");
var p3 = document.getElementById("player3");
var p4 = document.getElementById("player4");
var selections = [];
selections.push(p1, p2, p3, p4);

//traversal all the selections, if any not empty, remove its values under all other selections
var selected = [];
var unselected = [];

for(var i=0;i<selections.length;i++)
{
if(selections[i].value!="empty")
{
var x = selections[i].selectedIndex;
selected.push(x);
}
else
{
unselected.push(selections[i]);
}
}//end of for loop

for(var i=0;i<unselected.length;i++)
{
unselected[i].remove(selected[selected.length-1]);
}
}
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="./style.css">
<script>
</script>
</head>
<body>
<header>
<h1>New Players</h1>
</header>
<div>
<form action="./players.php" method="POST">
Player1: <input type="text" value="Player1">
<select id="player1" name="player1" onchange="removeOption()">
<option value="empty" disabled selected>--Please select--</option>
<option value="east">east</option>
<option value="south">south</option>
<option value="west">west</option>
<option value="north">north</option>
</select>
<br>
Player2: <input type="text" value="Player2">
<select id="player2" name="player2" onchange="removeOption()">
<option value="empty" disabled selected>--Please select--</option>
<option value="east">east</option>
<option value="south">south</option>
<option value="west">west</option>
<option value="north">north</option>
</select>
<br>
Player3: <input type="text" value="Player3">
<select id="player3" name="player3" onchange="removeOption()">
<option value="empty" disabled selected>--Please select--</option>
<option value="east">east</option>
<option value="south">south</option>
<option value="west">west</option>
<option value="north">north</option>
</select>
<br>
Player4: <input type="text" value="Player4">
<select id="player4" name="player4" onchange="removeOption()">
<option value="empty" disabled selected>--Please select--</option>
<option value="east">east</option>
<option value="south">south</option>
<option value="west">west</option>
<option value="north">north</option>
</select>
<br>
</form>
</div>
</body>
</html>

最佳答案

始终保留这四个选项会更加用户友好。

如果用户在 player3 中选择了一个已在 player1 中使用的选项(例如),只需重置 player1.

这也大大简化了您的代码。

removeOption 替换为 updateOptions(this)

function updateOptions(element) {
// select all the players but the current one
var players = document.querySelectorAll("select:not(#" + element.id + ")");
// iterate just that players
for (var i = 0; i < players.length; i++) {
// if the option was already selected
if (players[i].value == element.value) {
// just remove it
players[i].options[0].selected = true;
}
}
}
<html>

<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="./style.css">
<script>
</script>
</head>

<body>
<header>
<h1>New Players</h1>
</header>
<div>
<form action="./players.php" method="POST">
Player1: <input type="text" value="Player1">
<select id="player1" name="player1" onchange="updateOptions(this)">
<option value="empty" disabled selected>--Please select--</option>
<option value="east">east</option>
<option value="south">south</option>
<option value="west">west</option>
<option value="north">north</option>
</select>
<br> Player2: <input type="text" value="Player2">
<select id="player2" name="player2" onchange="updateOptions(this)">
<option value="empty" disabled selected>--Please select--</option>
<option value="east">east</option>
<option value="south">south</option>
<option value="west">west</option>
<option value="north">north</option>
</select>
<br> Player3: <input type="text" value="Player3">
<select id="player3" name="player3" onchange="updateOptions(this)">
<option value="empty" disabled selected>--Please select--</option>
<option value="east">east</option>
<option value="south">south</option>
<option value="west">west</option>
<option value="north">north</option>
</select>
<br> Player4: <input type="text" value="Player4">
<select id="player4" name="player4" onchange="updateOptions(this)">
<option value="empty" disabled selected>--Please select--</option>
<option value="east">east</option>
<option value="south">south</option>
<option value="west">west</option>
<option value="north">north</option>
</select>
<br>
</form>
</div>
</body>

</html>

关于javascript - 有没有一种简单的方法来进行 HTML 关系选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52922444/

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