Javascript jQuery Tokeninput和OnFreeTagingAdd回调

Javascript jQuery Tokeninput和OnFreeTagingAdd回调,javascript,jquery,jquery-tokeninput,Javascript,Jquery,Jquery Tokeninput,jQuery插件Tokeninput(主分支)()有一个用于添加标记的新特性。不幸的是,到目前为止,这项功能最好记录在Twitter上: 我试图弄清楚如何使用onFreeTagingAdd,但不幸的是,我是一个jQuery和javascript新手 简而言之,我希望回调从我的api获取输出,并在tokenbox中使用它。通过这种方式,我可以修改标记(小写等)并添加一个id。如果api建议使用另一个id/标记,我还可以将其替换为另一个id/标记 下面,请查看我目前的代码。我尝试了几个选项来设置it

jQuery插件Tokeninput(主分支)()有一个用于添加标记的新特性。不幸的是,到目前为止,这项功能最好记录在Twitter上:

我试图弄清楚如何使用onFreeTagingAdd,但不幸的是,我是一个jQuery和javascript新手

简而言之,我希望回调从我的api获取输出,并在tokenbox中使用它。通过这种方式,我可以修改标记(小写等)并添加一个id。如果api建议使用另一个id/标记,我还可以将其替换为另一个id/标记

下面,请查看我目前的代码。我尝试了几个选项来设置item=data并返回该值,但迄今为止没有成功。感谢您的帮助

onFreeTaggingAdd: function (item) {

$.post("../php/add_tagg_02.php", {tag: item, userid: "userid-dummy"} )
.done(function(data, status, xhr) {
alert ("Your suggested new tag " + data.name + " is entered in the database and will be considered for future use.");
console.log( data.name ); //returns the "new" name from the api
console.log( data.id ); //returns the id provided by the api
})
    return item; //returns the "old" name from the user input   
},

您可以通过编程方式添加和删除令牌,如示例所示:

   $(document).ready(function() {
    $("#demo-input-plugin-methods").tokenInput("http://shell.loopj.com/tokeninput/tvshows.php");
                // Add a token programatically
                $("#plugin-methods-add").click(function () {
                    $("#demo-input-plugin-methods").tokenInput("add", {id: 999, name: "James was here"});
                    return false;
                });
// Remove a token programatically
            $("#plugin-methods-remove").click(function () {
                $("#demo-input-plugin-methods").tokenInput("remove", {name: "James was here"});
                return false;
            });
   });

我相信库将freetag的
ID
设置为与其
name
相同的默认值,唯一的更改方法是在库中编辑
add\u freetaging\u tokens()
方法

在调用时,令牌只不过是一个字符串,因此要更改名称,您只需执行以下操作:

onFreeTaggingAdd: function (item) {

$.post("../php/add_tagg_02.php", {tag: item, userid: "userid-dummy"} )
.done(function(data, status, xhr) {
alert ("Your suggested new tag " + data.name + " is entered in the database and will be considered for future use.");
console.log( data.name ); //returns the "new" name from the api
console.log( data.id ); //returns the id provided by the api
})
    return data.name; //Sets the tokens Name/ID to be the new name from the api.
},
如果您确实希望自定义ID,那么更改方法以返回对象而不是字符串应该相对简单


另外请注意,除非您同时更新数据源,否则显然无法搜索此令牌。

这确实有效!代码非常干净。但是我希望没有必要这样删除和添加:'onFreeTaggingAdd:function(item){$.post(../php/add_tagg_02.php),{tag:item,userid:“userid dummy”}.done(function(data,status,xhr){alert(“Message”);$(“#tags”).tokenInput(“remove”,{name:“crap”})$(“#tags”).tokenInput”(“remove”),{id:data.id,name:data.name});})返回“crap”;},'我认为您是对的,因为如果脚本能够处理这些操作,您可以直接通过脚本
add_tagg_02.php
更改标记。