更改 Select2 以显示'n of m items selected'



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

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

n 个选定的 m 个项目

当选择超过 3 个项目时。单击该框后,您将看到确切的项目,就像现在一样。

然后,选择框可以呈现得更小,占用更少的空间。

这似乎不是一个配置选项。您是否有一个想法,在代码中的何处更改此行为?

你可以像这样轻松完成。不需要强奸选择2:)

$("#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.')
});

吉斯菲德尔

https://select2.github.io/examples.html#events

通过事件,您可以做到。触发选择事件并更新文本。

最新更新