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
欢迎关注本站公众号,获取更多信息