C# autocomplete
前臺代碼javascript
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title></title>
- <script src="jquery-1.10.2.min.js"></script>
- <link href="jquery-ui-1.12.1.custom/jquery-ui-1.12.1.custom/jquery-ui.css" rel="stylesheet" /> // 須要引的文件
- <script src="jquery-ui-1.12.1.custom/jquery-ui-1.12.1.custom/jquery-ui.min.js"></script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <input type="text" id="ado"/> // 文本框
- </div>
- </form>
- </body>
- <script type="text/javascript">
- $(function () {
- $("#ado").autocomplete({
- minLength: 1,
- // 經過函數來獲取並處理數據源
- source: function (request, response) { // 這裏的request表明須要傳的東西,response是爲了將數據展現給autocomplete
- $.post("hander.ashx", { data: request.term }, function (msg) { // 這裏ajax異步請求數據返回一個json串
- var dd = eval("(" + msg + ")"); // 這裏將json字符串裝換成json對象
- var arry = new Array(); //聲明瞭一個數組
- $.each(dd.data, function (i, list) { //遍歷json對象把json裏的數據添加到數組裏
- arry.push(list.Cname)
- });
- response($.map(arry, function (item) { return { value: item } })); //把數組轉換成 value:item 格式,而後給response展現出來
- });
- }
- });
- });
- </script>
- </html>
至於獲後臺數據就不寫了,大家本身寫吧!css
歡迎關注本站公眾號,獲取更多信息