这是我的代码:
$("#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 函数中,谁能帮助我