javascript - 选择多个/所有选项时,选择的多项选择变得巨大

标签 javascript jquery jquery-chosen

在多选字段上使用 Chosen.js 时,如果用户选择的选项超过 500 个,列表就会变得非常长。

enter image description here

有什么方法可以限制显示元素的数量吗? For example when chosing over 3 options, the user will have (4) options are selected..., instead of them being listed.

enter image description here

我想知道为什么他们的文档中没有这样的选项。

提前致谢。

最佳答案

你可以这样使用:

$('.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/

相关文章:

javascript - 为什么 "sort"对所有数组进行排序?

php - 需要帮助从 javascript 中的 onclick 函数保存文件

javascript - 如果 ID 部分匹配,则选择一个元素

jQuery UI 选项卡和对话框 - 如何基于对话框插件确认切换选项卡?

javascript - 选择的插件不适用于表中动态创建的行

html - 下拉框在页面外渲染,当窗口宽度增加时不关闭

jquery 选择插件 - 使用 php 获取数据

javascript - Js 正则表达式搜索问题。电子邮件检查

javascript - 使用 jQuery Mobile 时警报导航到主页

javascript - 将 $.extend 与用户定义的对象一起使用