下拉元素我試圖讓用戶挑「YES」,而另一個下拉列表會自動進入「NO」,反之亦然。我有一個代碼在下面,我不知道爲什麼它不工作。JS - 依賴
*我試圖得到它的onload,因此無需按一個按鈕,用戶會自動運行。
代碼:
<!DOCTYPE html>
<html>
<body>
<script>
const cmicrophone = document.querySelector('.select1');
const microphone = document.querySelector('.select2');
function inputHandler(thisSelect, otherSelect) {
// console.log(thisSelect.selectedIndex)
if (thisSelect.selectedIndex == 1) {
otherSelect.selectedIndex = 1;
} else if (thisSelect.selectedIndex == 2) {
otherSelect.selectedIndex = 2;
} else {
thisSelect.selectedIndex = 0;
otherSelect.selectedIndex = 0;
}
}
cmicrophone.addEventListener('input', event => {
inputHandler(cmicrophone, microphone);
});
microphone.addEventListener('input', event => {
inputHandler(microphone, cmicrophone);
});
</script>
<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 ">YES</option>
<option value="'S microphone is currently off. Please remind them to turn it ">NO</option>
</select>
</div>
<div class="microphone" id="microphone">Microphone:
<select id="microphone" class="microphone select2" name = "microphone">
<option value=" " selected="selected"> </option>
<option value="ON. Thank you.">ON</option>
<option value="OFF. Thank you.">OFF</option>
</select>
</div>
<div class="button updatebutton">
<button onclick="inputHandler(thisSelect, otherSelect)">UPDATE</button>
</div>
</body>
</html>
我需要調用什麼? jQuery的?它不適用於我仍然不知道我在做什麼這是錯的。 – catung555555
不需要'jquery'。複製我的代碼並粘貼你的html ..它的工作..哪裏有錯誤發生你能告訴嗎? – prasanth
我更新了我的代碼和我從你的代碼複製的代碼。當我嘗試運行代碼段的錯誤是:「消息」:「遺漏的類型錯誤:無法讀取屬性空的‘的addEventListener’」, 「文件名」:「http://stacksnippets.net/js」, 「LINENO 「:30, ‘colno’:14 } – catung555555