【问题标题】:jquery autocomplete resultsjQuery自动完成结果
【发布时间】:2009-11-04 21:51:42
【问题描述】:

我有一个网络服务

[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? 我想将它传递给另一个函数。

【问题讨论】:

    标签: jquery autocomplete


    【解决方案1】:

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

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

    【讨论】:

    • 我需要哪个 js 才能让它工作?因为我得到错误“对象不支持....” 10X
    • 你需要 jquery.autocomplete.js 和 jquery.js。
    • 没有仍然相同的错误......但我尝试使用:formatItem:function(data,i,max){ debugger;返回数据.Desc ; }, formatResult: function(data, data2) { //调试器; //alert(data) } 并且它可以工作,但是数据 allwaya 只包含“描述”而不是“ID”,这可能是因为我只用 datakey 调用自动完成:“描述”?
    【解决方案2】:

    我找到了我需要的东西

    Formatting data for jQuery Autocomplete results

    10 倍

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

    【讨论】:

      猜你喜欢
      • 2011-01-04
      • 2011-09-08
      • 2011-07-21
      • 2010-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      相关资源
      最近更新 更多