Set pipe as the delimiter for Select2

狂风中的少年 提交于 2020-01-03 04:45:22

问题


I have one multiple select dropdown, and I want to set pipe (|) as the delimiter.

For example, when a user selects multiple options (like Option1, Option2 and Option3) then it should separate (delimit) the values like this:

Option1|Option2|Option3

Below is my code:

$("#granttype").select2({ allowClear: true, tags: true, tokenSeparators: ['|'] });

I wrote this in the $document.ready() function.


回答1:


You have to use option tokenSeparators

$("#selectbox").select2({
  tags: true,
  tokenSeparators: ['|']
})



回答2:


incase your problem is not resolved yet, here is the answer! On older versions of select2 you got to use 'separator' instead of 'tokenSeparators, like this.

jQuery('selector').select2({
                            separator: ";",
                            allowClear:true,
                            ...
                            ...
                               });

Although as a fall back if you're not sure about the version of select2, the preferred way is to keep both the options, like this.

jQuery('selector').select2({
                            tokenSeparators: [';'],
                            separator: ";",
                            allowClear:true,
                            ...
                            ...
                               });



回答3:


For who still looking this,
This is script that working for me :

$(element).select2({
  tags: [],
  separator: "|"
});

The "tags: []," must be included, otherwise it give errors
Hope it help



来源:https://stackoverflow.com/questions/34721014/set-pipe-as-the-delimiter-for-select2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!