2013-04-11 28 views
0

這是我的代碼:獲得tagLabel jQuery的標籤,它

$("#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函數任何人都可以幫我

回答

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

我發現這在文檔check it out

也似乎應該啓用allowNewTags選項TRUE,也許這是導致一個錯誤,因爲你想插入新的標籤,而該插件不允許,check options too