Domanda

I'm using a really awesome plugin named jQuery Tagit in the development of my current project.

Everything was going (suspiciously) smooth until I attempted to add my own wee bit of jQuery. My goal was to give the end users the option to use a button to add a tag. I put some test buttons to see if it works.

The issue? The code I developed works to paste the needed term, but the targeted input isn't seeing it as me entering a tag. It'll sit there for a few seconds then just delete automatically. Below is the code I am using so far:

HTML:

<input value="PHP" id="PHP" type="button" class="addIt">
<input value="Perl" id="Perl" type="button" class="addIt">
<input value="Java" id="Java" type="button" class="addIt">
<input value="Asp" id="Asp" type="button" class="addIt">

<ul id="demo1" data-name="nameOfSelect"></ul>
<div class="buttons">
    <button id="demo1GetTags" value="Get Tags">Get Tags</button>
</div>
<div id="recordtags">
    <h2>Tag Records</h2>
</div>

jQuery:

$(function () {

var availableTags = [
    "ActionScript",
    "AppleScript",
    "Asp",
    "BASIC",
    "C",
    "C++",
    "Clojure",
    "COBOL",
    "ColdFusion",
    "Erlang",
    "Fortran",
    "Groovy",
    "Haskell",
    "Java",
    "JavaScript",
    "Lisp",
    "Perl",
    "PHP",
    "Python",
    "Ruby",
    "Scala",
    "Scheme"
];

$('#demo1').tagit({
    tagSource:availableTags,
    select:true,
    sortable:true,
    allowNewTags:false,
    triggerKeys:['enter', 'comma','tab']
});

$("#demo1GetTags").click(function () { showTags($("#demo1").tagit("tags")) });

function showTags(tags) {
    console.log(tags);
    var string = "";
    for (var i in tags)
        string += tags[i].value + ",";
    var result = string.slice(0,-1);
    $('#recordtags').append( result+"<br>" );
}

//MY CLICK FUNCTION THAT ISN'T WORKING PROPERLY
$(".addIt").click(function(){
    var selectorTarget = "#demo1";

    var specificInput = " .tagit-new input.tagit-input";
    var where2paste = selectorTarget+specificInput;
    $( where2paste ).focus();
    var value = this.id;
    var pastefield = where2paste;
    var input = $(pastefield);
    input.val(input.val() + value + ',');
    return false;
});

setInterval("$('#fork').effect('pulsate', { times:1 }, 500);", 5000);
});

Everything in my code works exactly as needed except for that one needed bit of functionality. :( So my question is this: what things do I need to tweak or revamp to help me add tags with the use of an input button? Thank you so much in advance. (e hugs)

È stato utile?

Soluzione

You can simply use the tagit add method.

Code:

//MY CLICK FUNCTION THAT ISN'T WORKING PROPERLY
$(".addIt").click(function () {
    $('#demo1').tagit('add', this.id);
    return false;
});

Demo: http://jsfiddle.net/q87Hb

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top