Jquery 标记和自动完成功能

Jquery 标记和自动完成功能,jquery,tag-it,Jquery,Tag It,代码如下: $("#AddFriendToGroup").tagit({ autocomplete: { source: function (request, response) { $.ajax({ type: 'POST', url: 'ChatPageTes

代码如下:

$("#AddFriendToGroup").tagit({
                 autocomplete: { 
                    source: function (request, response) {
                        $.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) {
                            },
                            error: function (xhr) {
                                alert("responseText: " + xhr.responseText);
                            }
                        });
                    },
                        minLength: 2
                    }
                });
服务器端:

public static string tagFriendAutocomplete(int ClientID,string ClientName)
{
    string Result = "";
    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++)
       {
           Result += dt.Rows[i]["FRIEND_ID"] + "," + dt.Rows[i]["USER_NAME"];
        }
    }

    return Result;
}
公共静态字符串tagFriendAutocomplete(int-ClientID,string-ClientName)
{
字符串结果=”;
string query=“从客户端选择fr.FRIEND\u ID,c.[USER\u NAME]在c.CLIENT\u ID=fr.FRIEND\u ID和fr.CLIENT\u ID=“+ClientID+”和c.[USER\u NAME]上像“%”“+ClientName+“%”一样内部加入friends fr;
DataTable dt=new SQLHelper(SQLHelper.ConnectionString.WebSiteConnectionString.getQueryResult(查询);
如果(dt.Rows.Count>0)
{ 

对于(int i=0;i获取有关在tagit中使用$.ajax()的信息,请参见

这实际上有点棘手,我不记得在文档中看到了详细的答案

我所做的是:

$("#[id of tag to attach to]").tagit({
autocomplete: {
    source: function( request, response ) {
        if (acAjax && acAjax.readyState != 4)
            acAjax.abort();

        $("#loading").show();

        acAjax = $.ajax({
            url: [server url],
            dataType: "json",
            data: {
                term: request.term
            },
            success: function( data ) {
                returnedUsers = data;
                response( $.map( data, function( item ) {
                    return {
                        label: item,
                        value: item
                    }
                }));
            },
            error: function(xhr, status, error) {
                returnedUsers = [];
            },
            complete: function(xhr, status, error) {
                $("#loading").hide();
            }
        });
    },
    minLength: 2
},
allowSpaces: true,
beforeTagAdded: function(event, ui) {
    if ($.inArray(ui.tagLabel, returnedUsers)==-1)
        return false;
}
});
很抱歉格式错误。请注意:

  • success'response'函数——这是一个tagit能够理解的函数。我不完全确定它是如何工作的,但请注意,使用这种格式,您可以让标记打印一件东西(“标签”),并让值为其他东西

  • “returnedUsers”数组允许我阻止用户添加ajax未返回的标记。这有点难以理解,因此我希望这对您有所帮助


  • 其他选项列在文档中,因此我不会深入讨论。希望这对您有所帮助。简单地指向文档是没有帮助的——我知道!

    这是一个很好的答案,但我有一个简单的问题,即响应包含一个标签和一个值,如果我想将这些id存储在隐藏字段中,wish应该是客户端的id我能做到这一点吗?