gpt4 book ai didi

jquery - 样式 select2 多选与单选相同?

转载 作者:行者123 更新时间:2023-11-28 08:57:34 25 4
gpt4 key购买 nike

基本上,我想要做的是让输入仅作为显示,当点击字段时,我希望下拉菜单的搜索字段与单选相同。 p>

下面的(做得不好的)模型比我能更好地解释这一点。

enter image description here

Select2 是否内置了此功能,或者是否有一种简单的方法可以做到这一点?

最佳答案

对于那些仍在寻找解决方案的人,请查看我的 fiddle 。

我已经使用了几个关于这个问题的 fiddle 并将它们组合成 1

不完美,但它有效。

https://jsfiddle.net/Lkkm2L48/7/

jQuery(function($) {
$.fn.select2.amd.require([
'select2/selection/single',
'select2/selection/placeholder',
'select2/selection/allowClear',
'select2/dropdown',
'select2/dropdown/search',
'select2/dropdown/attachBody',
'select2/utils'
], function (SingleSelection, Placeholder, AllowClear, Dropdown, DropdownSearch, AttachBody, Utils) {

var SelectionAdapter = Utils.Decorate(
SingleSelection,
Placeholder
);

SelectionAdapter = Utils.Decorate(
SelectionAdapter,
AllowClear
);

var DropdownAdapter = Utils.Decorate(
Utils.Decorate(
Dropdown,
DropdownSearch
),
AttachBody
);

var base_element = $('.select2-multiple2')
$(base_element).select2({
placeholder: 'Select multiple items',
selectionAdapter: SelectionAdapter,
dropdownAdapter: DropdownAdapter,
allowClear: true,
templateResult: function (data) {

if (!data.id) { return data.text; }

var $res = $('<div></div>');

$res.text(data.text);
$res.addClass('wrap');

return $res;
},
templateSelection: function (data) {
if (!data.id) { return data.text; }
var selected = ($(base_element).val() || []).length;
var total = $('option', $(base_element)).length;
return "Selected " + selected + " of " + total;
}
})

});

});

CSS:

.select2-results__option .wrap:before{
font-family:fontAwesome;
color:#999;
content:"\f096";
width:25px;
height:25px;
padding-right: 10px;

}
.select2-results__option[aria-selected=true] .wrap:before{
content:"\f14a";
}

关于jquery - 样式 select2 多选与单选相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14319095/

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