求助Jquery Autocomplete 如何动态加载数据,类似百度的搜索提示

2025-04-13 15:45:25
推荐回答(1个)
回答1:

1
2
3
4
5
6
85
86
87


88

89

静态填充


90
91

动态填充
92
93
94


95

96

97
98
后台代码:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Script.Serialization;
6 using System.Text;
7 using System.Web.Services;
8
9 namespace AutocompleteWeb
10 {
11 ///
12 /// ServerData 的摘要说明
13 ///

14 [WebService(Namespace = ]
15 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
16 public class ServerData : IHttpHandler
17 {
18
19 public void ProcessRequest(HttpContext context)
20 {
21 //GET方式
22 //context.Response.ContentType = "text/plain";
23 //string keyword = context.Request.QueryString["KW"];
24 //if (keyword != null)
25 //{
26 // 通过JavaScriptSerializer对象的Serialize序列化为["value1","value2",...]的字符串
27 //JavaScriptSerializer serializer = new JavaScriptSerializer();
28 //string jsonString = serializer.Serialize(GetProvinceCitys(keyword));
29 //context.Response.Write(jsonString); // 返回客户端json格式数据
30 //}
31
32 //POST方式
33 context.Response.ContentType = "application/json";
34 string keyStr = "";
35 if (context.Request["KW"] != null)
36 {
37 keyStr = context.Request["KW"].ToString();
38 }
39 ResponseJsionStr(context, GetProvinceCitys(keyStr));
40 }
41
42
43 ///
44 /// qinjue 2011-09-21
45 /// 返回jsion
46 ///

47 private void ResponseJsionStr(HttpContext context, string strJsion)
48 {
49 context.Response.Clear();
50 context.Response.ClearContent();
51 context.Response.Cache.SetNoStore();
52 context.Response.ContentType = "application/json";
53 context.Response.ContentEncoding = System.Text.Encoding.UTF8;
54 context.Response.Write(strJsion);
55 context.Response.End();
56 }
57
58 public string GetProvinceCitys(string KW)
59 {
60 StringBuilder cytySB = new StringBuilder();
61 cytySB.Append("[");
62 cytySB.Append("{\"name\":\"AACCASF东城区\",\"to\":\"11001\"},");
63 cytySB.Append("{\"name\":\"AACSAWE西城区\",\"to\":\"11002\"},");
64 cytySB.Append("{\"name\":\"AACAER海淀区\",\"to\":\"11003\"}");
65 cytySB.Append("]");
66 return cytySB.ToString();
67 }
68
69 public bool IsReusable
70 {
71 get
72 {
73 return false;
74 }
75 }
76 }
77 }