[英]Drop-down box dependent on the option selected in another drop-down box using jquery
如何获取其他父项下拉值或如何检查子项下拉值?
这是我的示例代码:
<select id="dropdown1" class="dropdown">
<option>val1</option>
<option>val2</option>
</select>
<select id="dropdown2" class="dropdown">
<option>val1</option>
<option>val2</option>
</select>
<select id="dropdown3" class="dropdown">
<option>val1</option>
<option>val2</option>
</select>
这是我的下拉菜单,我想检查父级下拉菜单的值是什么?
例如,如果我选择了dropdown3,那么dropdown 1 AND 2的值是多少
您可以声明一个具有所有下拉列表ID的变量:
var dropdowns = $( ".dropdown" ).map(function() {
return $( this ).attr('id');
});
聆听每个下拉列表的onchange:
$('.dropdown').change((e) => {
const dropdownId = e.target.id; // the changed dropdown
const otherDropdowns = dropdowns.filter((index, id) => id !== dropdownId); // get the others
const otherValues = otherDropdowns.map((index, id) =>
$('#' + id).find(":selected").val()); // get the values of others dropdowns
console.log(otherValues);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.