开发者

jquery autocomplete results

开发者 https://www.devze.com 2022-12-10 06:41 出处:网络
I have a webservice [Serializable] public class DataObject { public int ID { get; set; } public string Description { get; set; }

I have a webservice

[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 { Descr开发者_开发百科iption = 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 });

the question is: how can I get in javascript the ID of the item selected in the autocomplete? I want to pass it to another function.


You need to add a results handler:

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


i found what i needed in

Formatting data for jQuery Autocomplete results

10X

and the event is "result" not "results" .... 10X man !!

0

精彩评论

暂无评论...
验证码 换一张
取 消