14

我正在使用引导标签输入

我正在尝试动态添加标签。

$('.div-tag').tagsinput('add', 'vino');

上面的代码工作正常,但是当我尝试下面的代码时:

$('.div-tag').tagsinput('add', { id: 1, text: 'some tag' });

我得到错误:

Uncaught Can't add objects when itemValue option is not set

请帮我添加带有 id 和 value 的标签。

4

5 回答 5

18

初始化标签输入,如

 $('.div-tag').tagsinput({
      allowDuplicates: false,
        itemValue: 'id',  // this will be used to set id of tag
        itemText: 'label' // this will be used to set text of tag
    });

添加动态标签

$('.div-tag').tagsinput('add', { id: 'tag id', label: 'tag lable' });

就是这样;

于 2015-04-17T16:11:44.117 回答
13

我花了几个小时才发现,只有当data-role="tagsinput"从你的

<input class="div-tag" />
于 2015-06-09T21:10:10.503 回答
6

这个图书馆坏了,不要打扰。接受的答案不适用于 0.8 版。在尝试了几件事 1 小时后,我建议切换到另一个库,我使用了 tagEditor,在第一次尝试时一切正常。

于 2017-05-02T11:35:07.350 回答
2

该库没有任何问题,但是当您收到此错误时,这意味着方法初始化存在问题。

当页面准备好时尝试这样做:

$(document).ready(function(){
   $('#myInput').tagsinput({
       allowDuplicates: false,
       itemValue: 'id',
       itemText: 'label'
    });
});

$(".someButton").click(function(){
   $('#myInput').tagsinput('add', {id:1, label:"blah blah"});
}
于 2018-03-27T11:38:51.123 回答
-4

请尝试这样:

<div data-src="<?php echo get_template_directory_uri(); ?>/images/index_slide01.jpg">
    <div class="fadeIn camera_caption">
        <h2 class="text_1 color_1">Solutions that you need!</h2>
        <a class="btn_1" href="#">More info</a>
    </div>
</div>
于 2015-12-08T11:55:13.083 回答