英文:
How to select dropdown option when the same dropdown option is selected in other dropwown?
问题
这里有两个下拉菜单,每个下拉菜单都有不同的ID。我应该如何选择在这两个下拉菜单中选择的相同的值/选项?
英文:
Here I’ve 2 dropdowns
<label for="cars">Choose a car:</label>
<select name="cars" id="cars1">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<label for="cars">Choose a car:</label>
<select name="cars" id="cars2">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
2 different IDs for each dropdown: how do I select the same dropdown value/option which is selected in either of the dropdowns?
答案1
得分: 1
I understand that you only want the translated content, so here it is:
Not sure if I understood your question correctly...
$(document).on('change', '[name="cars"]', function(){
$('[name="cars"]').val( $(this).val() );
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="cars">Choose a car:</label>
<select name="cars" id="cars1">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<br><br>
<label for="cars">Choose a car:</label>
<select name="cars" id="cars2">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
Is there anything else you need?
英文:
Not sure if I understood your question correctly...
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).on('change', '[name="cars"]', function(){
$('[name="cars"]').val( $(this).val() );
})
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="cars">Choose a car:</label>
<select name="cars" id="cars1">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<br><br>
<label for="cars">Choose a car:</label>
<select name="cars" id="cars2">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论