[英]3 select elements that change options based on the selected option in the first and second select
[英]How to get auto selected same numeric option in a second select as that selected in first select?
Bootstrap 和 JavaScript 的新功能。
<select id="list1" name="list1" class="form-control" required/>
<?php
for ($h = 1; $h <= 7; $h++) echo "<option value='$h'>$h</option>";
?>
</select>
<select id="list2" name="list2" class="form-control" required/>
<?php
for ($h = 1; $h <= 7; $h++) echo "<option value='$h'>$h</option>";
?>
</select>
我有 2 個選擇,每個選項值 1-7。 當用戶在 list1 中選擇一個數字時,我希望在 list2 中自動選擇相同的數字。
謝謝
您可以使用HTMLSelectElement
的selectedIndex
屬性:
const firstList = document.getElementById('list1'); const secondList = document.getElementById('list2'); firstList.addEventListener('change', () => { secondList.selectedIndex = firstList.selectedIndex; });
<select id="list1" name="list1" class="form-control" required> <option value='1'>1</option> <option value='2'>2</option> <option value='3'>3</option> </select> <select id="list2" name="list2" class="form-control" required> <option value='1'>1</option> <option value='2'>2</option> <option value='3'>3</option> </select>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.