管理杂谈OA答疑ERP答疑教程搜索

jQuery 搜索关键字自动匹配提示插件


在现在的web设计中,提高用户体验是企业最为注重的内容之一。在搜索表单中,根据输入的部分内容进行关键字匹配提示功能,就是最直观和常用的交互体验,类似功能已经被多数的互联网网站应用。例如google的搜索框效果如下:http://bbon.cn/wp-content/uploads/2009/11/image_thumb8.png

这里介绍一个jquery实现搜索关键字自动匹配提示方法。jquery autocomplete 是一个基于jquery实现搜索关键字自动匹配提示的插件,该插件可扩展性强,表现性能优越,方便整合到自己的项目中使用;兼容ie 6.0+, ff 2+, safari 2.0+, opera 9.0+, and chrome 1.0+ 等主流浏览器。 演示效果:http://devbridge.com/projects/autocomplete/jquery/#demo

下面是具体的使用方法:

1,使用设置

首页,要把插件的js代码嵌入到你自己的项目中去。

2,使用方法

为要实现自动匹配提示的 input 表单添加 autocomplete 功能。

初始化 autocomplete 对象,确保正确加载 dom 对象,否则ie下的用户可能会出现错误。

$('#query').autocomplete({ serviceurl: 'service/autocomplete.ashx', // page for processing autocomplete requests minchars: 2, // minimum request length for triggering autocomplete delimiter: /(,|;)\s*/, // delimiter for separating requests (a character or regex) maxheight: 400, // maximum height of the suggestion list, in pixels width: 300, // list width zindex: 9999, // list's z-index deferrequestby: 0, // request delay (milliseconds), if you prefer not to send lots of requests while the user is typing. i usually set the delay at 300 ms. params: { country: 'yes'}, // additional parameters onselect: function(data, value){ }, // callback function, triggered if one of the suggested options is selected, lookup: <'january', 'february', 'march'> // list of suggestions for local autocomplete });

根据文本表单中的输入信息,进行关键字提示匹配。

{ query:'li', // original request suggestions:<'liberia','libyan arab jamahiriya','liechtenstein','lithuania'>, // list of suggestions data:<'lr','ly','li','lt'> // optional parameter: list of keys for suggestion options; used in callback functions. }

jquery autocomplete 插件支持 on/off功能,从而控制效果的开关。

var ac = $('#query').autocomplete({ /*parameters*/ }); ac.disable(); ac.enable(); ac.setoptons({ zindex: 1001 }); 3,设置表现样式

最后,用div和css美化表现效果。

liberia
libyan arab jamahiriya
liechtenstein
lithuania
.autocomplete-w1 { background:url(img/shadow.png) no-repeat bottom right; position:absolute; top:0px; left:0px; margin:6px 0 0 6px; /* ie6 fix: */ _background:none; _margin:1px 0 0 0; } .autocomplete { border:1px solid #999; background:#fff; cursor:default; text-align:left; max-height:350px; overflow:auto; margin:-6px 6px 6px -6px; /* ie6 specific: */ _height:350px; _margin:0; _overflow-x:hidden; } .autocomplete .selected { background:#f0f0f0; } .autocomplete div { padding:2px 5px; white-space:nowrap; overflow:hidden; } .autocomplete strong { font-weight:normal; color:#3399ff; } jquery autocomplete 插件官方链接 插件介绍: devbridge.com/projects/autocomplete/jquery/ 插件下载: devbridge.com/projects/autocomplete/jquery/#download

这里还有一种基于mootools框架的关键字自动匹配提示方法:

mootools: ajaxdaddy.com/mootools-autocomplete.html

更多精彩文章浏览...
点击右上角图标分享到朋友圈
官方网站:http://www.clicksun.cn
咨询热线:400-186-1886
服务邮箱:service@clicksun.cn