gpt4 book ai didi

javascript - JS 脚本 - 值相关(多个下拉列表)

转载 作者:行者123 更新时间:2023-12-03 04:40:51 25 4
gpt4 key购买 nike

现在我有两个下拉菜单,目标是让用户在第一个选项上选择“关闭”,第二个下拉菜单自动选择“打开”,反之亦然。

下面的 JavaScript 代码目前可以工作,但仅适用于一对下拉列表。我想知道我需要改变什么才能使其在未来适用于两对甚至四对。

代码:

<!DOCTYPE html>
<html>
<body>
<div class="cmicrophone" id="cmicrophone">Currently:
<select id="cmicrophone" class="cmicrophone select1" name="cmicrophone" onchange="">
<option value="">Select Option</option>
<option value="'S microphone is currently on. Please remind them to turn it ">OFF</option>
<option value="'S microphone is currently off. Please remind them to turn it ">ON</option>
</select>
</div>
<div class="microphone" id="microphone">Microphone:
<select id="microphone" class="microphone select2" name = "microphone">
<option value=" " selected="selected">Select Option</option>
<option value="ON. Thank you.">ON</option>
<option value="OFF. Thank you.">OFF</option>
</select>
</div>


<br>

<div class="cmicrophone" id="cmicrophone">Currently:
<select id="cmicrophone" class="cmicrophone select1" name="cmicrophone" onchange="">
<option value="">Select Option</option>
<option value="'S microphone is currently on. Please remind them to turn it ">OFF</option>
<option value="'S microphone is currently off. Please remind them to turn it ">ON</option>
</select>
</div>
<div class="microphone" id="microphone">Microphone:
<select id="microphone" class="microphone select2" name = "microphone">
<option value=" " selected="selected">Select Option</option>
<option value="ON. Thank you.">ON</option>
<option value="OFF. Thank you.">OFF</option>
</select>
</div>
</form>
</div>
<script>
const cmicrophone = document.querySelectorAll('.select1')[0];
const microphone = document.querySelectorAll('.select2')[0];
function inputHandler(thisSelect, otherSelect) {
otherSelect.selectedIndex =thisSelect.selectedIndex
}
cmicrophone.addEventListener('change', event => {
inputHandler(cmicrophone, microphone);
});

microphone.addEventListener('change', event => {
inputHandler(microphone, cmicrophone);
});
</script>
</body>
</html>

最佳答案

这是您的解决方案

<!DOCTYPE html>
<html>
<body>
<div class="cmicrophone" id="cmicrophone">Currently:
<select id="cmicrophone" class="cmicrophone select1" name="cmicrophone" onchange="">
<option value="">Select Option</option>
<option value="'S microphone is currently on. Please remind them to turn it ">OFF</option>
<option value="'S microphone is currently off. Please remind them to turn it ">ON</option>
</select>
</div>
<div class="microphone" id="microphone">Microphone:
<select id="microphone" class="microphone select2" name = "microphone">
<option value=" " selected="selected">Select Option</option>
<option value="ON. Thank you.">ON</option>
<option value="OFF. Thank you.">OFF</option>
</select>
</div>


<br>

<div class="cmicrophone" id="cmicrophone">Currently:
<select id="cmicrophone" class="cmicrophone select1" name="cmicrophone" onchange="">
<option value="">Select Option</option>
<option value="'S microphone is currently on. Please remind them to turn it ">OFF</option>
<option value="'S microphone is currently off. Please remind them to turn it ">ON</option>
</select>
</div>
<div class="microphone" id="microphone">Microphone:
<select id="microphone" class="microphone select2" name = "microphone">
<option value=" " selected="selected">Select Option</option>
<option value="ON. Thank you.">ON</option>
<option value="OFF. Thank you.">OFF</option>
</select>
</div>
</form>
</div>
<script>
var cmicrophone = document.querySelectorAll('.select1');
var microphone = document.querySelectorAll('.select2');

function inputHandlerCmicrophone(thisSelect, otherSelect, j) {

var test = document.querySelectorAll('.select1');
for(i=0; i<test.length; i++){
if(test[i] == j){
otherSelect[i].selectedIndex =thisSelect[i].selectedIndex
break;
}
}


}
function inputHandlerMicrophone(thisSelect, otherSelect, j) {

var test = document.querySelectorAll('.select2');
for(i=0; i<test.length; i++){
if(test[i] == j){
otherSelect[i].selectedIndex =thisSelect[i].selectedIndex
break;
}


}}
for(i=0; i<cmicrophone.length; i++) {
cmicrophone[i].addEventListener('change', function (e) {
inputHandlerCmicrophone(cmicrophone, microphone, e.target);
});
}

for(k=0; k<microphone.length; k++) {
microphone[k].addEventListener('change', function (e) {
inputHandlerMicrophone(microphone, cmicrophone, e.target);
});
}

</script>
</body>
</html>

关于javascript - JS 脚本 - 值相关(多个下拉列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43098839/

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