gpt4 book ai didi

jquery - selected-select 显示组标签和选项选择

转载 作者:行者123 更新时间:2023-12-01 07:42:22 24 4
gpt4 key购买 nike

我选择了使用 optdorup 和相同选项进行选择,我想在所选选择中显示 optgroup 标签和所选选项。请检查我的代码并提出更改建议。 检查这个 fiddle

http://jsfiddle.net/x5q95a11

$(".chosen-select").chosen();

$(".chosen-select").on('change', function (event,el) {
var selected_element = $(".chosen-select option:contains("+el.selected+")");

var selected_value = selected_element.val();
var parent_optgroup = selected_element.closest('optgroup').attr('label');

selected_element.text(parent_optgroup+' - '+selected_value).trigger("chosen:updated");
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://harvesthq.github.io/chosen/chosen.jquery.js"></script>

<select data-placeholder="Your Favorite Football Team" class="chosen-select" tabindex="5">
<option value=""></option>
<optgroup label="NFC EAST">
<option value="Dallas Cowboy">Dallas Cowboys</option>
<option value="New York">New York Giants</option>
<option value="Philadelphia">Philadelphia Eagles</option>
<option value="Washington">Washington Redskins</option>
<optgroup>
<optgroup label="NFC NORTH">
<option value="Dallas Cowboy">Dallas Cowboys</option>
<option value="New York">New York Giants</option>
<option value="Philadelphia">Philadelphia Eagles</option>
<option value="Washington">Washington Redskins</option>
</optgroup>

</select>

最佳答案

尝试插件提供的选项。 http://jsfiddle.net/kajalc/n1cnc53c/

$(".chosen-select").chosen({include_group_label_in_selected:true});

关于jquery - selected-select 显示组标签和选项选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45490791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com