2014-03-31 72 views
0

我正在寻找一个解决方案来添加标签到输入标签字段。Select2标签:动态添加标签到输入

我知道可以预加载标签,但我找不到向输入添加标签的方法。即使用特定的功能,我可以追加选择(带ID和TEXT)。

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

这是我的初始化:

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

分享您的代码开始...... – pbenard

+0

完成。非常怀疑它会帮助解决我的问题。 – FooBar

+0

你可以怀疑,或阅读:http://stackoverflow.com/help/how-to-ask – pbenard

回答

0

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

的基本语法

$('#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 ... } 
}