jquery自动完成结果

时间:2009-11-03 11:58:36

标签: jquery autocomplete

我有一个网络服务

[Serializable]
    public class DataObject
    {
        public int ID { get; set; }
        public string Description { get; set; }
    }


    [WebMethod]
    public DataObject[] GetCities(string q, int limit)
    {
        // A collection to hold our results
        List<DataObject> customers = new List<DataObject>();

        // Our source of names, could be a DB query
        string[] db = new string[]{"aaa","bbb","ccc","ddd","ddsads","asdsad","asdsad","dsfsfd"};

        // Looping through the datasource to select the items that match
        int i=0;
        foreach(string cust in db)
        {

            if(cust.ToLower().StartsWith(q.ToLower()))
            {
                i++;
                customers.Add(new DataObject { Description = cust, ID = i });
            }
        }

        // Return the items that contained the text in alphabetical order
        return customers.ToArray();

    }

使用Javascript:

// and in my javascript I use jquery autocomplete like this 
$("#txtCity").autocomplete("Autocomplete.asmx/GetCities", { dataType: "xml", datakey: "Description", max: 5 });

问题是: 如何在javascript中获取自动填充中所选项目的ID? 我想将它传递给另一个函数。

2 个答案:

答案 0 :(得分:2)

您需要添加结果处理程序:

$("#txtCity").autocomplete("<Your existing stuff here>").result(
         function(event, item, formatted)
         {
            // Use the item property
         }
      );

答案 1 :(得分:0)

我找到了我需要的东西

Formatting data for jQuery Autocomplete results

10X

事件是“结果”而不是“结果”.... 10X男人!!