英文:
How to select a particular dropdown when an option is selected in other dropdown?
问题
以下是翻译好的内容:
两个下拉框具有类似的选项,但在选项中有一个变化。在第一个下拉框中存在“Audi”,但在第二个下拉框中不存在。
当所选的选项为所需选项时,这会选择每个下拉框中的相同选项。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="cars">选择一辆车:</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">选择一辆车:</label>
<select name="cars" id="cars2">
<option value="">请选择</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
</select>
$(document).on('change', '[name="cars"]', function(){
$('[name="cars"]').val($(this).val());
})
当我选择“Audi”选项时,如何在保留当前功能的同时选择“请选择”选项。
英文:
2 dropdowns with similar options but 1 change in options. In the first dropdown Audi exists but not in second dropdown.
This selects the same option in each dropdowns when selected as required.
<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="">Please-Select</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
</select>
$(document).on('change', '[name="cars"]', function(){
$('[name="cars"]').val( $(this).val() );
})
When I select the “Audi” option, how do I select “Please-Select” option while retaining the current functionality.
答案1
得分: 1
根据您的评论,您需要同时连接两个选择框。
我已为两个选择框添加了ID,以便更轻松地进行选择,并为目标选择框添加了一个属性。然后,您可以测试其目标选择框中的选定选项。如果存在,选择它。如果不存在,则选择默认选项。
通过这种针对现有匹配的测试方式,可以添加值,而无需担心第二个选择框或差异。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="cars1">Choose a car:</label>
<select name="cars1" id="cars1" data-target="cars2">
<option value="">Please-Select</option>
<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="cars2">Choose a car:</label>
<select name="cars2" id="cars2" data-target="cars1">
<option value="">Please-Select</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="mclaren">McLaren</option>
</select>
$(document).ready(function(){
$('select').on('change', function() {
// 选定选择框的值
var selected_select = $(this).attr('id');
// 目标选择框的值
var selected_target = $(this).attr('data-target');
// 选项的值
var selectedValue = $(this).val();
// 检查目标值是否存在(true / false)
var targetedValue = $('#' + selected_target + ' option[value="' + selectedValue + '"]').length > 0;
$('#' + selected_select + ' option[value="' + selectedValue + '"]').prop('selected', true);
// 如果值存在(true),则将其标记为选定,否则选择默认选项
if(targetedValue === true) {
$('#' + selected_target + ' option[value="' + selectedValue + '"]').prop('selected', true);
} else {
$('#' + selected_target + ' option[value=""]').prop('selected', true);
}
});
});
请注意,这是您提供的HTML和JavaScript代码的翻译部分。
英文:
As per you comment, you need both selects to be linked in/out.
I have added IDs to both selects for a easier selection, and an attribute for the targeted select. Then you test the selected option in its targeted select. If it exists, select it. If not, select the default option.
This way of testing against an existing match will allow to add values without worrying about the second select or differences.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function(){
$('select').on('change', function() {
// the value of the selected select
var selected_select = $(this).attr('id');
// the value of the targeted select
var selected_target = $(this).attr('data-target');
// the value of the option
var selectedValue = $(this).val();
// check if the targeted value exists (true / false)
var targetedValue = $('#'+selected_target+' option[value='+selectedValue+']').length > 0;
$('#'+selected_select+' option[value='+selectedValue+']').prop('selected', true);
// if value exists (true) then mark it as selected, else, pick the default option
if(targetedValue === true) {
$('#'+selected_target+' option[value='+selectedValue+']').prop('selected', true);
} else {
//$('#'+selected_target+' option[value=""]').attr('selected','selected');
$('#'+selected_target+' option[value=""]').prop('selected', true);
}
});
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="cars1">Choose a car:</label>
<select name="cars1" id="cars1" data-target="cars2">
<option value="">Please-Select</option>
<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="cars2">Choose a car:</label>
<select name="cars2" id="cars2" data-target="cars1">
<option value="">Please-Select</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="mclaren">McLaren</option>
</select>
<!-- end snippet -->
答案2
得分: 0
以下是您要翻译的内容:
更改了选择器,以通过ID
选择。
将所选值保存在一个变量中,当值更改时,检查是否为audi,然后第二个select="cars"
上的选择值将为空值,即Please-select
。
注意:
- 在页面加载时添加了一个新的函数,以更改第一个
select
的值为第一个。删除此函数不会影响您的要求。随意修改它。 for
属性用于定位与label
相关的输入,它必须是目标input
的id
,这是一个唯一的值,所以我将for
属性的值更改为匹配inputs
的id
。- 请记住,从技术上讲,在HTML表单中可能存在两个具有相同
name
属性的输入。但是,当提交表单时,这可能导致意外行为。
代码:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function(){
var selectedValue = $('[id="cars1"]').val();
$('[id="cars2"]').val(selectedValue === 'audi' ? '' : selectedValue);
});
$(document).on('change', '[id="cars1"]', function(){
var selectedValue = $(this).val();
$('[id="cars1"]').val(selectedValue);
$('[id="cars2"]').val(selectedValue === 'audi' ? '' : selectedValue);
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<label for="cars1">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="cars1">Choose a car:</label>
<select name="cars" id="cars2">
<option value="">Please-Select</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
</select>
<!-- end snippet -->
请注意,这是您提供的代码和注释的翻译。
英文:
Changed the selectors, to select by ID
.
Saved the selected value in a variable, and on change, checking if the value is audi, then the selected value on the second select="cars"
will be the empty value, which is Please-select
.
NOTES:
- Added a new function when the page is loaded, to change the value of the second
select
as the first one. Deleting this function won't affect your requirements. Feel free to modify it as you like. - The
for
attribute is to target the input that related tolabel
, and it has to be theid
of the targetinput
which is a unique value, so I changed thefor
attributes values to match theid
's of theinputs
.
<label for="cars1">Choose a car:</label>
<select name="cars" id="cars1">
...
</select>
<label for="cars2">Choose a car:</label>
<select name="cars" id="cars2">
...
</select>
- Keep in mind, that technically, it is possible to have two inputs with the same
name
attribute in an HTML form. However, it can lead to unexpected behavior when submitting the form.
The Code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function(){
var selectedValue = $('[id="cars1"]').val();
$('[id="cars2"]').val(selectedValue === 'audi' ? '' : selectedValue);
});
$(document).on('change', '[id="cars1"]', function(){
var selectedValue = $(this).val();
$('[id="cars1"]').val(selectedValue);
$('[id="cars2"]').val(selectedValue === 'audi' ? '' : selectedValue);
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<label for="cars1">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="cars1">Choose a car:</label>
<select name="cars" id="cars2">
<option value="">Please-Select</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
</select>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论