0

这是我的代码:

$("#AddFriendToGroup" + GroupID).tagit({
                    allowDuplicates: false,
                    readOnly: false,
                    autocomplete: { source: function (request, showChoices) {
                        $.ajax({
                            type: 'POST',
                            url: 'ChatPageTest.aspx/tagFriendAutocomplete',
                            data: "{'ClientID':'" + $("#UserID").val() + "','ClientName': '" + request.term + "'}",
                            contentType: 'application/json; charset=utf-8',
                            dataType: 'json',
                            success: function (data) {
                                returndData = data;
                                showChoices($.map(data.d, function (item) {

                                    return {
                                        label: item.split('-')[0],
                                        val: item.split('-')[1]
                                    }
                                }))
                            },
                            error: function (xhr) {
                                alert("responseText: " + xhr.responseText);
                            }
                        });
                    }
                    },
                    beforeTagAdded: function (event, ui) {

                        if ($.inArray(ui.tagLabel, returndData) == -1) return false;
                    },
                    minLength: 2
                }); // tagit

服务器端 :

public static string[] tagFriendAutocomplete(int ClientID,string ClientName)
{
    List<string> Friends = new List<string>();
    string query = "select fr.FRIEND_ID,c.[USER_NAME] from clients c inner join friends fr on c.CLIENT_ID=fr.FRIEND_ID and fr.CLIENT_ID=" + ClientID + " and c.[USER_NAME] like '%" + ClientName + "%' ";

    DataTable dt = new SQLHelper(SQLHelper.ConnectionStrings.WebSiteConnectionString).getQueryResult(query);
    if (dt.Rows.Count > 0)
    { 
       for(int i=0;i<dt.Rows.Count;i++)
       {
          Friends.Add(string.Format("{0}-{1}",dt.Rows[i]["USER_NAME"], dt.Rows[i]["FRIEND_ID"]));
        }
    }

    return Friends.ToArray();
}

我的问题是当我尝试从自动完成建议中添加新标签时,不会添加新标签我想我的问题出在 beforeTagAdded 函数中,谁能帮助我

4

1 回答 1

0
.tagit("add", {label: 'tag', value: 12})

我在文档中找到了这个检查一下

此外,您似乎应该启用allowNewTags选项TRUE,也许这会导致错误,因为您尝试插入新标签而插件不允许这样做,请检查选项

于 2013-04-11T09:39:49.000 回答