Question

J'ai un champ de balise d'entrée, et je veux obtenir l'ID des Tages sélectionnés, donc j'ai essayé http://jsfiddle.net/u8zj5/19/ Mais mon problème, je veux faire passer que l'identification de l'identification ou de la valeur ne passe pas id="show" mais j'ai échoué.

<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']
    });
});

});

Quelqu'un ici qui a utilisé jQuery Tagit (page de démonstration) Pourrait m'aider à résoudre ce problème

Était-ce utile?

La solution

J'ai eu le même problème et j'ai modifié tag-it.js.Lorsque vous appelez la fonction select, vous devez envoyer le ID grâce à la fonction _addTag

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

Ensuite, il vous suffit d'obtenir l'identifiant :

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

Et ici, ajoutez le ID sur une sélection cachée

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

Avec cela, vous pouvez avoir une étiquette pour la liste de saisie semi-automatique, une valeur à afficher dans la balise et le ID sur une sélection cachée.

Autres conseils

J'ai eu le même problème, mais je ne voulais pas modifier le comportement par défaut du plugin.Donc, à la place, j'ai utilisé les hooks fournis pour ajouter un nouveau comportement.

var availableTags = [
    {
        label: "myTag",
        value: "myTag",
        id: 1
    },
    //etc...
];
var assignedTags = [];


$('#singleinputfield').tagit( {
                            tagSource: function (request, response) {
                                    //setup the search to search the label
                                    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                                    response($.grep(availableTags, function (value) {
                                        return matcher.test(value.label);
                                    }));
                            },
                            beforeTagAdded: function(event, ui){
//get id for that tag and signal if it was in the tagSource list
                                var id, result = false;
                                $.each(availableTags, function(){
                                    if(ui.tagLabel === this.label){
                                        result = true;
                                        id=this.id;
                                        return false;
                                    }
                                });
                                if(result){
//put id in the list of ids we are using
                                    assignedTags.push(id);
                                }
                                return result;
                            },
                            afterTagAdded: function(event, ui){
//replace the values in the single input field with the assigned ids
                                    $('#singleinputfield').val(assignedTags.join(','));
                            },
                            afterTagRemoved: function(event, ui){
                                $('#singleinputfield').val(assignedTags.join(','));
                            },
                            beforeTagRemoved: function(event, ui){
                                var id;
//get the id for the removed tag and signal if it was in the tagSource list
                                $.each(availableTags, function(){
                                    if(ui.tagLabel === this.label){
                                        result = true;
                                        id = this.id;
                                        return false;
                                    }
                                });
                                if(result){
//remove the unassigned tag's id from our list
                                    assignedTags = $.grep(assignedTags, function(el){
                                        return el != id;
                                    });
                                }
                            }
                        });

Utilisez un autre plugin comme Select2.Il bénéficie en fait d'un soutien pour cela.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top