【发布时间】:2020-10-28 14:52:56
【问题描述】:
因为我有一个选择元素 optgroup,其值也与之绑定。这是我在下面的结构。
<select id="example-dataprovider-optgroups" multiple="multiple">
<optgroup label="Lime No. 2" value="b3a2eff6-5351-4b0f-9861-0d47e136517d">
<option value="90b4365b-9ddc-4c08-9e42-03662d73d923" label="Chimneys and Towers"></option>
<option value="6a7d30d8-e500-476f-a2c6-7adfb47a3e00" label="Height Safety"></option>
<option value="eb89ab4a-0431-4ed2-b6ba-a0c1bc91b0f0" label="Lightning Protection"></option>
</optgroup>
<optgroup label="Lime No. 4" value="42da4f3e-1944-4f42-a5b7-350871cbffea">
<option value="90b4365b-9ddc-4c08-9e42-03662d73d923" label="Chimneys and Towers"></option>
<option value="6a7d30d8-e500-476f-a2c6-7adfb47a3e00" label="Height Safety"></option>
<option value="eb89ab4a-0431-4ed2-b6ba-a0c1bc91b0f0" label="Lightning Protection"></option>
</optgroup>
<optgroup label="Lime No. 1" value="46ec4dec-e669-4829-b99a-5ac64340eb84">
<option value="90b4365b-9ddc-4c08-9e42-03662d73d923" label="Chimneys and Towers"></option>
<option value="6a7d30d8-e500-476f-a2c6-7adfb47a3e00" label="Height Safety" selected="selected"></option>
<option value="eb89ab4a-0431-4ed2-b6ba-a0c1bc91b0f0" label="Lightning Protection"></option>
</optgroup>
</select>
现在我正在使用如下所示的引导多选。
$('#example-dataprovider-optgroups').multiselect({
enableFiltering: false,
enableClickableOptGroups: true,
includeSelectAllOption: true,
});
使用 optgroup 作为可点击。现在我需要 optgroup 的值以及它的 option 值。我尝试使用下面的方法。
$("#example-dataprovider-optgroups").each(function (index, item) {
console.log(index);
console.log(item);
$(item).find('li').each(function (index1, item2) {
console.log(item2);
});
//console.log(item.find("multiselect-item multiselect-group active"));
//console.log(item.find("option:selected"));
})
我怎样才能从两者中获取价值..?
更新 1:
$("#example-dataprovider-optgroups").find("optgroup").each(function (index, item) {
console.log(index);
console.log(item);
console.log($(item).find(":selected")); // i don't get selected value from optgroup
})
【问题讨论】:
-
在您的代码示例中,每个选项组下的所有选项都是相同的,但实际上,每个选项都会有一个唯一的 GUID 作为值吗?如果是,则可以找到选项组。否则你就不走运了。
标签: javascript jquery bootstrap-4 bootstrap-multiselect