Jquery Autocomplete()/Catcomplete()函数不起作用

Jquery Autocomplete() / Catcomplete() Function not working

本文关键字:函数 不起作用 Catcomplete Jquery Autocomplete      更新时间:2023-09-26

当我使用以下代码从web服务中获取catcomplete自动完成intellisense时,我无法看到它。我的web服务返回以下字符串

[{label:"TechCrunch",category:"Website"},{label:"Techcrunch",category:"Tag"},      {label:"techno",category:"Tag"}] 

 $(document).ready(function() {
       $("#<%= txtinputtag.ClientID%>").catcomplete({
      source: function(request,response){//error if I hard code this ajax call will with a an array works fine 
               $.ajax({
                   type: "POST",
                   contentType: "application/json; charset=utf-8",
                   url: "../Tag/Follow.aspx/GetIntellisense",
                   dataType: "json",
                   data: "{'searchtext':'" + request.term + "'}",
                   success: function (data) {
                       if (data.d != "") {
                           //console.log(data.d);//this show the desired json output as returned from the web service
                            response(data.d);
                       }
                   }
               });
           },
           select: function(event, ui) {
               $(this).val("");
               return false;
           },
       });
   });

这是返回字符串的WebMethod

 [WebMethod]
        public static string GetIntellisense(string searchtext)
    {
        Debugger.Launch();
        var uc = new UtilityClass();
        List<DTOWebsite> lstDtoWebsites = uc.GetIntellisense(searchtext);
        string str = "[";
        foreach (DTOWebsite dto in lstDtoWebsites)
        {
            str += "{label:'""+dto.WebSiteName +"'",category:'""+dto.WebsiteType +"'"},";
        }
        str = str.Remove(str.Length-1,1);
        str += "]";
        return str.ToString();
    }

您需要在分配给source参数的函数中接受两个参数:

  1. request具有用于搜索的term

  2. response是将结果传递给的回调函数

    source: function(request, response){ ...
    

http://jsfiddle.net/h7uahuhx/