gpt4 book ai didi

javascript - 在选择框中进行选择并将所选答案显示到另一个选择框中

转载 作者:行者123 更新时间:2023-11-28 02:05:10 24 4
gpt4 key购买 nike

我需要你的帮助,

我不确定如何进行以下操作:

如果我在第一个选择框中选择绿色,我需要将所选选项自动选择到第二个选择框中。

如何仅使用 JavaScript 来完成此任务。

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<span>Category:</span>
<br>
<select id="select1">
<option value=""></option>
<option value="RED">RED</option>
<option value="BLUE">BLUE</option>
<option value="GREEN">GREEN</option>
<option value="YELLOW">YELLOW</option>
</select>
<br>
<span>Your selection was:</span>
<br>
<select id="select2">
<option value=""></option>
<option value="RED">RED</option>
<option value="BLUE">BLUE/option>
<option value="GREEN">GREEN</option>
<option value="YELLOW">YELLOW</option>
</select><b></b>
</body>
</html>

最佳答案

您可以迭代选项,直到找到相同的值:

document.getElementById("select1").onchange = function() {
var selected = this.value;
var select2 = document.getElementById("select2");

//find the index in the second select
for (var i = 0; i < select2.options.length; i++) {
if (select2.options[i].value == selected) {
select2.options[i].selected = true;
}
}
}

演示:http://jsfiddle.net/HJm7E/

关于javascript - 在选择框中进行选择并将所选答案显示到另一个选择框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17883484/

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