jquery autocomplete使用教程(动态绑定以及ajax实现)
2013-07-10 00:00
1306 查看
注意我用的是jquery autocomplete plugin,不是jquery ui中的jquery autocomplete,jquery autocomplete plugin的使用很简单,如下:
说明如下:
formatItem作用在于可以格式化列表中的条目。formatMatch是配合formatItem使用,作用在于,由于使用了formatItem,所以条目中的内容有所改变,而我们要匹配的是原始的数据,所以用formatMatch做一个调整,使之匹配原始数据,formatResult是定义最终返回的数据,比如我们还是要返回原始数据,而不是formatItem过的数据。因为我动态clone了DOM,也可以动态绑定autocomplete:
使用ajax返回数据实现autocomplete也很简单:
jquery autocomplete 使用总结就这么多,有几个案例摘自网络。
<html> <head> <title>PHP点点通-www.phpddt.com</title> <script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="/js/jquery.autocomplete.min.js"></script> <link rel="Stylesheet" href="/js/jquery.autocomplete.css" /> <script type="text/javascript"> var emails = [ { name: "Molly", to: "molly@yahoo.com" }, { name: "mckee", to: "mckee@phpddt.com" }, ]; $(function() { $('#keyword').autocomplete(emails, { max: 12, //列表里的条目数 minChars: 0, //自动完成激活之前填入的最小字符 width: 400, //提示的宽度,溢出隐藏 scrollHeight: 300, //提示的高度,溢出显示滚动条 matchContains: true, //包含匹配,就是data参数里的数据,是否只要包含文本框里的数据就显示 autoFill: false, //自动填充 formatItem: function(row, i, max) { return i + '/' + max + ':"' + row.name + '"[' + row.to + ']'; }, formatMatch: function(row, i, max) { return row.name + row.to; }, formatResult: function(row) { return row.to; } }).result(function(event, row, formatted) { alert(row.to); }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <input id="keyword" /> <input id="getValue" value="GetValue" type="button" /> </div> </form> </body> </html>
说明如下:
formatItem作用在于可以格式化列表中的条目。formatMatch是配合formatItem使用,作用在于,由于使用了formatItem,所以条目中的内容有所改变,而我们要匹配的是原始的数据,所以用formatMatch做一个调整,使之匹配原始数据,formatResult是定义最终返回的数据,比如我们还是要返回原始数据,而不是formatItem过的数据。因为我动态clone了DOM,也可以动态绑定autocomplete:
<script type='text/javascript' src='js/jquery-autocomplete/jquery.autocomplete.js'></script> <link rel="stylesheet" type="text/css" href="js/jquery-autocomplete/jquery.autocomplete.css" /> <script> $(function(){ $("input.departs").live("keydown",function(){ //departs的json格式数据 $(this).autocomplete(<?php echo $departs?>, { width: 250, max: 100, scroll: true, scrollHeight: 300, formatItem: function(row, i, max) { return row.name; }, formatResult: function(row) { return row.fixed_number; } }).result(function(event, row) { $(this).parent().siblings().find("input[name='deputy_name[]']").val(row.contact_name); $(this).parent().siblings().find("input[name='fixed_number[]']").val(row.fixed_number); $(this).parent().siblings().find("input[name='mobile_phone[]']").val(row.mobile_phone); $(this).parent().siblings().find("input[name='email[]']").val(row.email); }); }); }); </script>
使用ajax返回数据实现autocomplete也很简单:
<script type="text/javascript"> $(document).ready(function () { $.ajax({ type: "POST", contentType: "application/json", url: "AjaxPage.aspx/GetAllHints", data: "{}", dataType: "json", success: function (msg) { var datas = eval('(' + msg.d + ')'); $("#txtIput").autocomplete(datas, { formatItem: function (row, i, max) { return "<table width='400px'><tr><td align='left'>" + row.Key + "</td><td align='right'><font style='color: #009933; font-family: 黑体; font-style: italic'>约" + row.Value + "www.phpddt.com</font> </td></tr></table>"; }, formatMatch: function(row, i, max){ return row.Key; } }); } }); }); </script>
jquery autocomplete 使用总结就这么多,有几个案例摘自网络。
相关文章推荐
- 使用JQuery快速实现Tab的AJAX动态载入
- 【JavaWeb开发】使用Jquery与Ajax实现动态联级菜单
- 使用JQuery快速实现Tab的AJAX动态载入(实例讲解)
- jquery autocomplete插件结合ajax使用demo
- Jquery、Json、Struts2、Spring实现Ajax动态功能以及注意事项
- 利用autocomplete.js实现仿百度搜索效果(ajax动态获取后端[C#]数据)
- jquery多选择动态参数的使用以及ajax异步请求中提交整个form表单中serializeArray()的使用以及后台值得获取方式
- 轻松掌握Ajax.net系列教程十五:使用AutoCompleteExtender
- JavaWeb开发之使用jQuery与Ajax实现动态联级菜单效果
- 使用jquery动态生成的标签,需要在代码当中绑定才可以实现事件的监听
- 轻松掌握Ajax.net系列教程十五:使用AutoCompleteExtender
- 使用jquery.unobtrusive-ajax实现无刷新查询以及优化改进
- jQuery UI Autocomplete 使用 ajax 方法传输Json数据出现乱码问题的解决
- JQuery autocomplete ajax 实现自动完成
- 使用JQuery快速实现Tab的AJAX动态载入(实例讲解)
- 利用JQuery+Ajax实现aspx页面无刷新的动态数据绑定
- jquery autocomplete插件结合ajax使用demo
- jquery autocomplete ajax获取动态数据,兼容各浏览器,支持中文
- 使用autoCompleteTextView以及MultiAutoCompleteTextView实现自动匹配输入内容
- 利用JQuery+Ajax实现aspx页面无刷新的动态数据绑定