jQuery Bootstrap 标签输入添加带有 id 和值的标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29703773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:37:28  来源:igfitidea点击:

Bootstrap tagsinput add tag with id and value

jquerybootstrap-tags-input

提问by Manish Shukla

I am using Bootstrap Tags Input

我正在使用Bootstrap 标签输入

I am trying to add tags dynamically.

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

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

The above code works fine, but when I try the following code:

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

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

I get the error:

我收到错误:

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

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

Please help me to add tag with id and value.

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

回答by Manish Shukla

initialize tags input like

初始化标签输入,如

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

To add dynamic tag

添加动态标签

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

That's it;

就是这样;

回答by Artemis909

I spent few hours to find out, that above working only when data-role="tagsinput"is removed from your

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

<input class="div-tag" />

回答by sivann

This library is broken, don't bother. The accepted answer doesn't work on version 0.8. After 1h of trying several things, I suggest to switch to another library, I used tagEditor, where everything worked on the 1st try.

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

回答by molham556

There is nothing wrong with the library, but when you get this error it means there is a problem with the method initilization.

库没有任何问题,但是当您收到此错误时,则表示方法初始化存在问题。

Try to do it when the page is ready like so:

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

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

$(".someButton").click(function(){
   $('#myInput').tagsinput('add', {id:1, label:"blah blah"});
}

回答by Md. Bozlur Rosid Shagor

Please try like this:

请尝试这样:

<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>