更改Select2以显示“已选择m个项目中的n个" [英] Change Select2 to display 'n of m items selected'

查看:92
本文介绍了更改Select2以显示“已选择m个项目中的n个"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在启用了标记的情况下使用Select2插件,例如以下示例: https://select2.github.io/examples.html#tags

I like to use the Select2 plugin with tagging enabled, like in this example: https://select2.github.io/examples.html#tags

但是我想更改它以显示类似

However I would like to change it to display a text like

已选择m个项目中的n个

n of m items selected

当有比说选择3项以上.单击该框后,您将看到准确的项目,就像现在一样.

when there are more than say 3 items selected. Once you click the box you would see the exact items, like it is now.

然后可以将选择框显示得更小,占用的空间更少.

Then the select box could be rendered much smaller and consume less space.

这似乎不是配置选项.您有什么主意,请在代码中的什么地方更改此行为?

This does not seem to be a config option. Do you have an idea, where in the code to change this behavior?

推荐答案

您可以像这样轻松完成.不需要强奸select2:)

You can do it easy like this. No rape select2 is needed :)

$("#singleSelectExample").select2({
  closeOnSelect: false
});

$('#singleSelectExample').on('change', function() {
  var selected = $(this).val().length; 
  var of = $(this).find('option').length;
  $(this).parent().find('.select2-selection ul').html('Selected ' + selected + ' of ' + of + '  items.')
});

JSFIDDLE

这篇关于更改Select2以显示“已选择m个项目中的n个"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆