我们如何使用Jquery tagit获取选定标签的ID? [英] How we can get the ID of selected tags using Jquery tagit?

查看:77
本文介绍了我们如何使用Jquery tagit获取选定标签的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入标签字段,我想获取所选标签的ID 所以我尝试 http://jsfiddle.net/u8zj5/19/但我的问题是获取ID而不是标签 或值传递到id="show",但我失败了.

I have a input tag field ,and I want to get the ID of the selected tages So I have try http://jsfiddle.net/u8zj5/19/ But my problem I want to get the id not label or value to pass into id="show" but I failed.

<input type="text" id="field1" name="field1" value=""/>
<span id="show">show ID here</span>

jQuery(document).ready(function(){
var availableTags = [{"id":"144","label":"Allicelabel","value":"Allice value"}];
jQuery("input#field1").each(function(){
    var target = jQuery(this);
    var currenttags = target.val();
    target.hide()        
          .after("<ul class=\"tags\"><li>"+currenttags+"</li></ul>");
    var instance = target.next();
    instance.tagit({
        tagSource:availableTags,
        tagsChanged:function () {
            var tags = instance.tagit('tags');
            var tagString = [];
            for (var i in tags){
                tagString.push(tags[i].value);
            }
            $("#show").html(tagString.join(','));
        },
        sortable:true,
        triggerKeys: ['enter', 'comma', 'tab']
    });
});

});

这里使用 jQuery Tagit(演示页面)的任何人帮我解决这个问题

Anyone here that used jQuery Tagit (Demo Page) Could help me to solve this

推荐答案

我遇到了同样的问题,我所做的是修改tag-it.js. 调用函数选择时,您需要通过函数_addTag

I had the same problem and I what did was modify tag-it.js. When you call the function select you need to send the ID through the function _addTag

self._addTag(ui.item.label, ui.item.value, ui.item.id);

然后您只需要获取ID:

Then youu just need to get the id:

_addTag: function(label, value, id) {
    ...
    this._addSelect(label, value, id);
    ...
}

在这里,将ID附加在隐藏的Select上

And here append the ID on a hidden Select

_addSelect: function(label, value, id) {
        var opt = $('<option>').attr({
            'selected':'selected',
            'value':id
        }).text(label);
        this.select.append(opt);

有了这个,您可以拥有一个自动完成列表标签,一个要在标签中显示的值以及一个隐藏选择中的ID.

With this you can have, one label for the autocomplete list, one value to show in the tag, and the ID on a hidden select.

这篇关于我们如何使用Jquery tagit获取选定标签的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆