Select2 jquery plugin show number of selected items in the result instead of tags

前端 未结 2 1003
猫巷女王i
猫巷女王i 2021-01-01 17:35

I am using Select2 jQuery Plugin.

https://select2.github.io/ for reference

When I am using the multiple dropdown option. The result of selec

2条回答
  •  盖世英雄少女心
    2021-01-01 17:52

    Selectors can certainly be improved, but as a blueprint, adding a counter element on change and hiding the tags like this seems to work as asked.

    $('select').select2({closeOnSelect: false}).on("change", function(e) {
      $('.select2-selection__rendered li:not(.select2-search--inline)').hide();
      $('.counter').remove();
      var counter = $(".select2-selection__choice").length;
      $('.select2-selection__rendered').after('
    '+counter+' selected
    '); });
    .counter{
      position:absolute;
      top:0px;
      right:5px;
     }
     .select2-search--inline{
       background-color:Gainsboro;
     }
    
    
    

提交回复
热议问题