C# autocomplete

前臺代碼javascript

  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>

  至於獲後臺數據就不寫了,大家本身寫吧!css

相關文章
相關標籤/搜索