27

I've been trying out the new Select2 v4.0 which has a lot of improvements. I'm mainly interested in the tags feature. I want to be able to search for tags via ajax and only be able to select a tag from the shown results and not be able to create new tags. The functionality is similar to StackOverflow - if you don't have the necessary reputation you can't create new tags, but you can still tag a question with existing tags.

Here's a jsfiddle with my code which is taken from the examples. In the example, you can create new tags which are what I want to limit. The user should be able to select tags only from the list that's retrieved from GitHub via ajax.

Does anybody know how to disable this functionality?

4

5 回答 5

49

这应该有效 - 在 select2 的初始化中,尝试从 createTag 函数返回 undefined ,如下所示:

createTag: function(params) {
                return undefined;
           }
于 2015-06-22T13:38:10.527 回答
23

我也一直在努力解决这个问题,但几个小时后就开始工作了。

我已经指定了一些标记分隔符(因为我的访问者可以在网站的不同位置创建标签)。事实证明,即使tags配置设置为,分隔符仍然适用false

解决方案:tags: false并且不要为tokenSeperators. 保持multiple: true

于 2015-06-29T19:09:01.447 回答
7

tags: true您可以在初始化 Select2 时通过删除来禁用标签。或者,tags: false在初始化 Select2 时进行设置。Tags are only enabled if the tagsoption is truthy , which it is when you are passing in true.

于 2015-05-27T01:52:18.630 回答
1

我不确定是否应该在此处添加以下内容,但是当我在搜索相同的问题时,谷歌向我指出了这个问题。但是,我使用的是旧版本 3.x,这就是如何为 3.x 版本实现相同的场景。

在 3.5 版本上测试

createSearchChoice: function(params) {
    return undefined;
}
于 2019-01-31T14:19:50.147 回答
0

标签:假,

$("#DropDownId").select2({
       minimumInputLength: 3,
       maximumSelectionLength: 10,
       tags: [],
       **tags:false,**
    ajax: {
        url: "@Url.Action("ActionName", "ControllerName")",
        type: "get",
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                Title: params.term // search term
            };
        },
        processResults: function (response) {
            return {
                results: $.map(response, function (item) {
                    return {
                        text: item.Title,
                        id: item.NibafInstituteId
                    }
                })
            };
        }
    }
});
于 2021-07-07T14:45:21.607 回答