【发布时间】:2015-09-29 10:02:11
【问题描述】:
我正在使用来自 https://github.com/aehlke/tag-it 的 tag-it jquery 插件
var ip_elem = $('#my-tags');
ip_elem.tagit({
removeConfirmation: false,
caseSensitive: false,
allowDuplicates: false,
allowSpaces: true,
readOnly: false,
tagLimit: null,
singleField: false,
singleFieldDelimiter: ',',
singleFieldNode: null,
tabIndex: null,
placeholderText:"Enter Tags"
});
现在,我想使用createTag 方法以编程方式添加更多标签。
var newTags = ["javascript","php","ruby","python"];
for(var k=0;k<newTags.length;k++){
ip_elem.tagit("createTag", newTags[k]);
}
它在输入中显示第一个标签,但对于第二个标签来说:Uncaught Error: cannot call methods on tagit prior to initialization; attempted to call method 'createTag'。它现在停止并且不再在输入框中添加任何标签。所以,最后只形成了第一个标签(在这种情况下是javascript)
这里出了什么问题?
【问题讨论】:
-
因为每次您都在循环中将
tagit初始化为ip_elem.. -
@GuruprasadRao :这就是他们在文档中显示的方式。你能看看吗? :github.com/aehlke/tag-it#createtagtaglabel-additionalclass
-
您会看到它们只显示单个标签,而不是多个标签。
-
@GuruprasadRao:是的。我看到。那么,您如何建议我添加数组中的所有标签?
-
@GuruprasadRao:我认为
tagit会自动检测它之前是否已初始化。
标签: javascript jquery jquery-plugins tag-it