【问题标题】:Select2 Tags: Dynamicly add tag to inputSelect2 标签:动态添加标签到输入
【发布时间】:2014-03-31 11:07:03
【问题描述】:

我正在寻找将标签添加到输入标签字段的解决方案。

我知道可以有预加载的标签,但我没有找到在输入中添加标签的方法。 IE。使用特定函数,我可以附加一个选择(带有 ID 和 TEXT)。

我已经搜索了很多,但也许我陷入了死循环。有什么建议吗?

这就是我的初始化方式:

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

【问题讨论】:

  • 分享您的代码以开始...
  • 完成。非常怀疑它会帮助解决我的问题。
  • 你可以怀疑,或者阅读这个:*.com/help/how-to-ask

标签: javascript php jquery jquery-select2


【解决方案1】:

正如我在这个相关问题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 ... }
}

【讨论】:

    最近更新 更多