在多选字段上使用 Chosen.js 时,如果用户选择的选项超过 500 个,列表就会变得非常长。
有什么方法可以限制显示元素的数量吗? For example when chosing over 3 options, the user will have (4) options are selected..., instead of them being listed.
我想知道为什么他们的文档中没有这样的选项。
提前致谢。
最佳答案
你可以这样使用:
$('.element').chosen().change(function() {
var totalSelected = $(this).find('option:selected').size();
var placeholder = $(this).find('option:first-child').text();
if(totalSelected > 3) {
$(this).next().find('.chosen-choices').find('li.search-choice').hide(),
$(this).next().find('.chosen-choices').find('.literal-multiple').show();
$(this).next().find('.chosen-choices').find('span.literal-multiple').text(placeholder + " ("+totalSelected+")");
}
});
类 literal-multiple
是一个自定义元素,用于显示 totalSelected
元素。您需要将其添加到所选插件的原型(prototype)中:
文件选择.jquery.js
Chosen.prototype.set_up_html = function() {
//stuff
if(this.is_multiple) {
var selVal = this.default_text;
this.container.html('<ul class="chosen-choices"><span class="literal-multiple"></span></ul>');
}
};
关于javascript - 选择多个/所有选项时,选择的多项选择变得巨大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32221919/