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

前端 未结 2 991
猫巷女王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:58

    You can add this code after initializing select2

    $('select').on('select2:close', function (evt) {
            var uldiv = $(this).siblings('span.select2').find('ul')
            var count = $(this).select2('data').length
            if(count==0){
              uldiv.html("")
            }
            else{
              uldiv.html("
  • "+count+" items selected
  • ") }

    Ref: jsfiddle
    Reference to select2 events: Here

    Edit:
    If you want to display a blank when user deselects everything, Use this fiddle: here

    Edit:
    Updated to remove flaw in deselection of data and changed it to main answer. Fiddle: here

提交回复
热议问题