C# autocomplete

前台代码

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  5. <title></title>
  6. <script src="jquery-1.10.2.min.js"></script>
  7. <link href="jquery-ui-1.12.1.custom/jquery-ui-1.12.1.custom/jquery-ui.css" rel="stylesheet" />   // 需要引的文件
  8. <script src="jquery-ui-1.12.1.custom/jquery-ui-1.12.1.custom/jquery-ui.min.js"></script>
  9. </head>
  10. <body>
  11. <form id="form1" runat="server">
  12. <div>
  13. <input type="text" id="ado"/> // 文本框
  14. </div>
  15. </form>
  16. </body>
  17. <script type="text/javascript">
  18. $(function () {
  19.   $("#ado").autocomplete({
  20.   minLength: 1,
  21.        // 通过函数来获取并处理数据源
  22.        source: function (request, response) {   // 这里的request代表需要传的东西,response是为了将数据展示给autocomplete
  23.        $.post("hander.ashx", { data: request.term }, function (msg) {      // 这里ajax异步请求数据返回一个json串
  24.              var dd = eval("(" + msg + ")");   // 这里将json字符串装换成json对象
  25.              var arry = new Array();     //声明了一个数组
  26.              $.each(dd.data, function (i, list) {   //遍历json对象把json里的数据添加到数组里
  27.              arry.push(list.Cname)
  28.        });
  29.       response($.map(arry, function (item) { return { value: item } }));  //把数组转换成 value:item 格式,然后给response展示出来
  30. });
  31. }
  32. });
  33. });
  34. </script>
  35. </html>

  至于获后台数据就不写了,你们自己写吧!

猜你喜欢

转载自www.cnblogs.com/damenge/p/8876256.html