2009-11-03 85 views
0

我有一個web服務jQuery的自動完成結果

[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

您需要添加一個結果處理程序:

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

哪個js我需要使它工作? 因爲我得到錯誤「對象不支持....」 10X – haddar 2009-11-03 13:40:30

+0

您需要jquery.autocomplete.js和jquery.js。 – GenericTypeTea 2009-11-03 13:57:29

+0

不能正常工作.... 對象不支持 任何想法爲什麼? – haddar 2009-11-04 09:18:40