Jquery AutoComplete详解
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Jquery AutoComplete详解,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含9728字,纯文字阅读大概需要14分钟。
内容图文
<SPAN style=‘font-family: "courier new", courier; font-size: medium;‘>什么是AutoComplete
<SPAN style=‘font-family: "courier new", courier; font-size: medium;‘>AutoComplete是Jquery库中的一种插件,属于Jquery UI中的一种.
<SPAN style=‘font-family: "courier new", courier; font-size: medium;‘> 官网地址:http://jqueryui.com/autocomplete/
<SPAN style=‘font-family: "courier new", courier; font-size: medium;‘>AutoComplete的作用
<SPAN style=‘font-family: "courier new", courier; font-size: medium;‘> AutoComplete控件就是当用户在文本框输入前几个字母或是汉字的时候,该控件就能从存放数据的文本或是数据库里将所有以这些字母开头的数据提示给用户,供用户选择,提供方便。
<SPAN
style=‘font-family: "courier new", courier; font-size: 16px;‘>一:jquery.autocomplete详解
语法:
<SPAN
style=‘font-family: "courier new", courier; font-size: 16px;‘>autocomplete(urlor
data, [options] )
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>参数:
<SPAN
style=‘font-family: "courier new", courier; font-size: 16px;‘>url or
data:数组或者url
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>[options]:可选项,选项解释如下:
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>1) minChars (Number)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>在触发autoComplete前用户至少需要输入的字符数,Default:1,如果设为0,在输入框内双击或者删除输入框内内容时显示列表。<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>2) width (Number)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>指定下拉框的宽度,Default: input元素的宽度
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>3) max (Number)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>autoComplete下拉显示项目的个数,Default: 10
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>4) delay (Number)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>击键后激活autoComplete的延迟时间(单位毫秒),Default: 远程为400 本地10
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>5) autoFill (Boolean)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>要不要在用户选择时自动将用户当前鼠标所在的值填入到input框,Default: false
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>6) mustMatch (Booolean)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>如果设置为true,autoComplete只会允许匹配的结果出现在输入框,所有当用户输入的是非法字符时将会得不到下拉框,Default:false
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>7) matchContains (Boolean)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>决定比较时是否要在字符串内部查看匹配,如ba是否与foo bar中的ba匹配.使用缓存时比较重要.不要和autofill混用.Default: false
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>8) selectFirst (Boolean)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>如果设置成true,在用户键入tab或return键时autoComplete下拉列表的第一个值将被自动选择,尽管它没被手工选中(用键盘或鼠标).当然如果用户选中某个项目,那么就用用户选中的值. Default: true
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>9) cacheLength (Number)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>缓存的长度.即对从数据库中取到的结果集要缓存多少条记录.设成1为不缓存.Default: 10
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>10) matchSubset (Boolean)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>autoComplete可不可以使用对服务器查询的缓存,如果缓存对foo的查询结果,那么如果用户输入foo就不需要再进行检索了,直接使用缓存.通常是打开这个选项以减轻服务器的负担以提高性能.只会在缓存长度大于1时有效.Default: true
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>11) matchCase (Boolean)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>比较是否开启大小写敏感开关.使用缓存时比较重要.如果你理解上一个选项,这个也就不难理解,就好比foot要不要到FOO的缓存中去找.Default: false
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>12) multiple (Boolean)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>是否允许输入多个值即多次使用autoComplete以输入多个值. Default:false
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>13) multipleSeparator (String)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>如果是多选时,用来分开各个选择的字符. Default:","
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>14) scroll (Boolean)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>当结果集大于默认高度时是否使用卷轴显示 Default: true
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>15) scrollHeight (Number)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>自动完成提示的卷轴高度用像素大小表示 Default: 180
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>16) formatItem (Function)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>为每个要显示的项目使用高级标签.即对结果中的每一行都会调用这个函数,返回值将用LI元素包含显示在下拉列表中.Autocompleter会提供三个参数(row, i, max): 返回的结果数组, 当前处理的行数(即第几个项目,是从1开始的自然数), 当前结果数组元素的个数即项目的个数.Default: none, 表示不指定自定义的处理函数,这样下拉列表中的每一行只包含一个值.<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>17) formatResult (Function)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>和formatItem类似,但可以将将要输入到input文本框内的值进行格式化.同样有三个参数,和formatItem一样.Default: none,表示要么是只有数据,要么是使用formatItem提供的值.
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>18) formatMatch (Function)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>对每一行数据使用此函数格式化需要查询的数据格式. 返回值是给内部搜索算法使用的. 参数值row
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>19) extraParams (Object)
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>为后台(一般是服务端的脚本)提供更多的参数.和通常的作法一样是使用一个键值对对象.如果传过去的值是{ bar:4 },将会被autocompleter解析成my_autocomplete_backend.php?q=foo&bar=4 (假设当前用户输入了foo). Default: {}
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>20) result (handler) Returns:jQuery
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>此事件会在用户选中某一项后触发,参数为:
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>event: 事件对象. event.type为result<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>.
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>data: 选中的数据行.
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>formatted:formatResult函数返回的值
<SPAN
style=‘font-family: "courier new", courier; font-size: 16px;‘>例如:
<SPAN
style=‘font-family: "courier new", courier; font-size: 16px;‘>$("#singleBirdRemote").result(function(event,
data, formatted){
<SPAN
style=‘font-family: "courier new", courier; font-size: 16px;‘>//如选择后给其他控件赋值,触发别的事件等等
<SPAN
style=‘font-family: "courier new", courier; font-size: 16px;‘>});<SPAN
style=‘font-family: "courier new", courier; font-size: 16px;‘>
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>二:jquery.autocomplete使用技巧
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>1) jquery.autocomplete需要的数据源可以为:本地数据和远程数据.
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>本地数据源为本地js数组或本地的json对象,如:
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>var data = ["c++","java", "php", "coldfusion","javascript"];
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>var data = [{text:‘Link A‘, url:‘/page1‘}, {text:‘Link B‘, url: ‘/page2‘} ];
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>在使用远程地址时,它默认传入的参数是:q(输入值),limit(返回结果的最大值),可以使用extraParams传入其他的参数,而且远程数据源是需要固定格式的数据,返回结果:使用“\n”分割每行数据,每行数据中使用“|”分割每个元素.
<SPAN style=‘font-family: "courier new", courier; font-size: 16px;‘>三:jquery.autocomplete使用实例
1 $("#tags").autocomplete(["c++","java", "php", "coldfusion","javascript"], {
2 width: 320,
3 max: 4,
4 highlight: false,
5 multiple: true,
6 multipleSeparator: "",
7 scroll: true,
8 scrollHeight: 300
9 });
1
var data = [{text:‘Link A‘, url:‘/page1‘}, {text:‘Link B‘, url: ‘/page2‘} ];
2 $("...").autocomplete(data,{
3 formatItem: function(item) {
4return item.text;
5}
6 }).result(function(event,item) {
7 location.href = item.url;
8 });
远程数据源调用方法:
一:
1 <scripttype="text/javascript">
2 $(document).ready(function() {
3 $("#Login").focus().autocomplete("/ajax/Account", {
4 formatItem: function(row, i,max) {
5var obj =eval("(" + row + ")"); //转换成js对象 6return obj.Text;
7},
8 formatResult: function(row) {
9var obj =eval("(" + row + ")"); //转换成js对象 10 return obj.Text;
11}
12 }).result(function(event, item) {
13var obj = eval("(" +item + ")"); //转换成js对象 14 $("#link").attr(“href”,obj.url);
15});
16});
17 </script>
后台C#代码(MVC)
1
public ContentResult GetUserAccount(string q)
2{
3string strResult = "";
4//...查询数据操作...
5//...格式化数据...
6//格式完成的结果为 7 strResult = "{text:‘Link A‘, url:‘/page1‘}\n {text:‘Link B‘, url: ‘/page2‘} ";
8return Content(strResult);
9 }
二:
1 <scripttype="text/javascript">
2 $(document).ready(function() {
3 $.getJSON("/ajax/Account",function(data) {
4 $("#Login").focus().autocomplete(data, {
5 formatItem: function(item,i, max) {
6return item.Text;
7},
8 formatResult:function(item) {
9return item.Text;
10}
11 }).result(function(event, item){
12 $("#link").attr(“href”,item.url);
13});
14});
15});
16 </script>
后台C#(MVC)代码
1 publicContentResult Account(string q)
2{
3string strResult = "";
4//...查询数据操作...
5//...格式化数据...
6//格式完成的结果为 7 strResult = "[{\"text\":\"LinkA\", \"url\":\"/page1\"}, {\"text\":\"LinkB\", \"url\": \"/page2\"} ]";
8return Content(strResult);
9 }
原文:http://www.cnblogs.com/MR-LIUB/p/3637018.html
内容总结
以上是互联网集市为您收集整理的Jquery AutoComplete详解全部内容,希望文章能够帮你解决Jquery AutoComplete详解所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。