Select2标签:动态添加标签到输入

时间:2014-03-31 11:07:03

标签: javascript php jquery jquery-select2

我正在寻找一种向输入标记字段添加标记的解决方案。

我知道可以预装标签,但我发现无法在输入中添加标签。即使用特定的函数,我可以附加一个选择(带ID和TEXT)。

我搜索了很多,但也许我处于死循环中。有什么建议吗?

这是我初始化的方式:

    $('.select2Input').select2({
        tags: {
            0: {
                id: 'the id',
                text: 'the text'
            }
        },
        multiple: true,
        minimumInputLength: 2
    });

1 个答案:

答案 0 :(得分:0)

正如我在这个相关问题select2 destroy and recreate中提到的,当您需要更改初始化选项时,诀窍在于销毁和重新创建select2。我怀疑这也是你在案件中需要做的事情。

基本语法是

$('#categoryid').select2("destroy");
createmycategoryidselect();

function createmycategoryidselect() {
   // standard options
   $opts = { ... }
   // gather the current set of tag values and stick them in this new instance.
   $opts.tags = { ... new tags ... }
}