1、jquery.autocomplete API
语法: autocomplete(url/data, [options] )
参数: url / data:url或者数组 [options]:可选项
2、[options]:可选项,选项解释如下:
minChars: 0, //至少输入的字符数,default:1;如果设为0,在输入框内双击或者删除内容时显示列表。 width: 220, //下拉框的宽度,default:input元素的宽度 max: 10, //下拉项目的个数,default:10 scrollHeight: 300, // 下拉框的高度, Default: 180 scroll: true, //当结果集大于默认高度时,是否使用滚动条,Default: true multiple: false, //是否允许输入多个值. Default: false autoFill: false, // 是否自动填充. Default: false multipleSeparator: " ", //输入多个字符时,用来分开各个的字符. Default: "," matchCase:false, //是否开启大小写敏感 selectFirst:true, // 如果设置成true,下拉列表的第一个值将被自动选择, Default: true matchSubset:true, //是否启用缓存 cacheLength: 10, //缓存的长度.即缓存多少条记录.设成1为不缓存.Default: 10 delay: 20, //击键后的延迟时间(单位毫秒).Default: 远程为400 本地10 mustMatch:false, //如果设置为true,只会允许匹配的结果出现在输入框,当用户输入的是非法字符时,将被清除, Default: false matchContains:true, //决定比较时是否要在字符串内部查看匹配.Default: false formatItem: function(row, i, max) { } //结果中的每一行都会调用这个函数,返回值将用LI元素包含,显示在下拉列表中. 三个参数(row, i, max): 返回的结果数组, 当前处理的行数(从1开始), 当前结果数组元素的个数. Default: none, 表示不指定自定义的处理函数. formatResult : function(row, i, max) { } //和formatItem类似,但可以将将要输入到input文本框内的值进行格式化.同样有三个参数,和formatItem一样.Default: none,表示要么是只有数据,要么是使用formatItem提供的值. formatMatch: function(row) { } //对每一行数据使用此函数格式化需要查询的数据格式. 返回值是给内部搜索算法使用的. 参数值row result (function(event, data, formatted){}) //此事件会在用户选中某一项后触发,参数为:event: 事件对象, data: 选中的数据行,formatted:formatResult函数返回的值; 例如: $("#d").result(function(event, data, formatted){alert(formatted);}) extraParams (Object): //为后台(一般是服务端的脚本)提供更多的参数.和通常的作法一样是使用一个键值对对象.如果传过去的值是{ bar:4 },将会被autocompleter解析成my_autocomplete_backend.php?q=foo&bar=4 (假设当前用户输入了foo). Default: {}
<!DOCTYPE HTML > <html> <head> <title>jquery.autocomplete.js 实例</title> <meta charset="utf-8"> <script type="text/javascript" src="js/jquery.js"></script> <script type='text/javascript' src='js/jquery.autocomplete.js'></script> <link rel="stylesheet" type="text/css" href="css/main.css" /> <link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" /> <style type="text/css"> p{margin:8px;} </style> </head> <body> <h1 id="banner"><a href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/">jQuery Autocomplete Plugin</a> Demo</h1> <div id="content"> <form autocomplete="off"> <p> <label>City1:</label> <input type="text" id="city1" /> </p> <p> <label>City2:</label> <input type="text" id="city2" /> </p> <p> <label>Tags:</label> <input type="text" id='tags' /> </p> <p> <label>E-Mail:</label> <input type="text" id="email" /> </p> </form> <h3>Result:</h3> <ol id="result"></ol> </div> </body> </html> <script type="text/javascript"> $(function() { var cities = [ "Aberdeen", "Ada", "Beaverdam", "Bedford","Cuyahoga Falls", "Dayton", "De Graff", "Fairfield", "Fairpoint", "Groveport", "Grover Hill","Hamden", "Hamersville", "Irondale", "Ironton", "Jacksontown","Kirby", "Kirkersville", "Lafayette", "Lafferty", "Munroe Falls", "Murray City", "Oberlin", "Oceola", "Paris", "Parkman", "Ravenna", "Rawson", "Saint Louisville", "Toledo", "Tontogany", "Wakeman", "Walbridge", "Yorkshire", "Yorkville", "Zoar" ]; var emails = [ { name: "Peter Pan", to: "peter@pan.de" }, { name: "Molly", to: "molly@yahoo.com" }, { name: "Forneria Marconi", to: "live@japan.jp" }, { name: "Master <em>Sync</em>", to: "205bw@samsung.com" }, { name: "Dr. <strong>Tech</strong> de Log", to: "g15@logitech.com" }, { name: "Don Corleone", to: "don@vegas.com" }, { name: "Mc Chick", to: "info@donalds.org" }, { name: "Donnie Darko", to: "dd@timeshift.info" }, { name: "Quake The Net", to: "webmaster@quakenet.org" }, { name: "Dr. Write", to: "write@writable.com" } ]; $("#tags").autocomplete(["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "pearl"]); $("#city1").autocomplete(cities); $("#city2").autocomplete(cities, { minChars: 0, //至少输入的字符数,default:1; width: 220, //下拉框的宽度,default:input元素的宽度 max: 10, //下拉项目的个数,default:10 scrollHeight: 300, // 下拉框的高度, Default: 180 scroll: true, //当结果集大于默认高度时,是否使用滚动条,Default: true multiple: false, //是否允许输入多个值. Default: false }); $("#email").autocomplete(emails, { minChars: 0, width: 310, matchContains: "word", 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; } }); function log(event, data, formatted) { $("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result"); } $(":text").result(log) //被选中的插入Result }); </script>