簡體   English   中英

如何在第二個 select 中自動選擇與第一個 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 中自動選擇相同的數字。

謝謝

您可以使用HTMLSelectElementselectedIndex屬性:

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM