jquery-autocomplete 使用手册

 
jquery-autocomplete 使用手册
2010-08-16 14:40

jquery-autocomplete学习

一、用前必备

官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/

当前版本:1.0.2

需要JQuery版本:1.2.6

二、使用

<script src="./jquery-1.3.2.js" type="text/javascript"></script>

<script src="./jquery.autocomplete.js" type="text/javascript"></script>

<link rel="stylesheet" href="./jquery.autocomplete.css" />

autocomplete( url or data, options ) Returns: jQuery

让一个input或textarea有autocomplete功能

例子

Js代码

var data = "Core Selectors Attributes Traversing Manipulation CSS Events Effects Ajax Utilities".split(" ");  

$("#example").autocomplete(data);

以上的例子就是为id为example添加autocomplete

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" }

];

//emails的数组格式如上,formatItem代表的是显示的格式,formatMatch表示匹配的内容,formatResult表示结果的内容

$("#suggest13").autocomplete(emails, {

    minChars: 0,

    width: 310,

    matchContains: true,

    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;

    }

});

三、参数说明:

* minChars (Number):

    在触发autoComplete前用户至少需要输入的字符数.Default: 1,如果设为0,在输入框内双击或者删除输入框内内容时显示列表

* width (Number):

    指定下拉框的宽度. Default: input元素的宽度

* max (Number):

    autoComplete下拉显示项目的个数.Default: 10

* delay (Number):

    击键后激活autoComplete的延迟时间(单位毫秒).Default: 远程为400 本地10

* autoFill (Boolean):

    要不要在用户选择时自动将用户当前鼠标所在的值填入到input框. Default: false

* mustMatch (Booolean):

    如果设置为true,autoComplete只会允许匹配的结果出现在输入框,所有当用户输入的是非法字符时将会得不到下拉框.Default: false

* matchContains (Boolean):

    决定比较时是否要在字符串内部查看匹配,如ba是否与foo bar中的ba匹配.使用缓存时比较重要.不要和autofill混用.Default: false

* selectFirst (Boolean):

    如果设置成true,在用户键入tab或return键时autoComplete下拉列表的第一个值将被自动选择,尽管它没被手工选中(用键盘或鼠标).当然如果用户选中某个项目,那么就用用户选中的值. Default: true

* cacheLength (Number):

    缓存的长度.即对从数据库中取到的结果集要缓存多少条记录.设成1为不缓存.Default: 10

* matchSubset (Boolean):

    autoComplete可不可以使用对服务器查询的缓存,如果缓存对foo的查询结果,那么如果用户输入foo就不需要再进行检索了,直接使用缓存.通常是打开这个选项以减轻服务器的负担以提高性能.只会在缓存长度大于1时有效.Default: true

* matchCase (Boolean):

    比较是否开启大小写敏感开关.使用缓存时比较重要.如果你理解上一个选项,这个也就不难理解,就好比foot要不要到FOO的缓存中去找.Default: false

* multiple (Boolean):

    是否允许输入多个值即多次使用autoComplete以输入多个值. Default: false

* multipleSeparator (String):

    如果是多选时,用来分开各个选择的字符. Default: ","

* scroll (Boolean):

    当结果集大于默认高度时是否使用卷轴显示 Default: true

* scrollHeight (Number):

    自动完成提示的卷轴高度用像素大小表示 Default: 180  

* formatItem (Function):

    为每个要显示的项目使用高级标签.即对结果中的每一行都会调用这个函数,返回值将用LI元素包含显示在下拉列表中. Autocompleter会提供三个参数(row, i, max): 返回的结果数组, 当前处理的行数(即第几个项目,是从1开始的自然数), 当前结果数组元素的个数即项目的个数. Default: none, 表示不指定自定义的处理函数,这样下拉列表中的每一行只包含一个值.

* formatResult (Function):

    和formatItem类似,但可以将将要输入到input文本框内的值进行格式化.同样有三个参数,和formatItem一样.Default: none,表示要么是只有数据,要么是使用formatItem提供的值.

* formatMatch (Function):

    对每一行数据使用此函数格式化需要查询的数据格式. 返回值是给内部搜索算法使用的. 参数值row

* extraParams (Object):

    为后台(一般是服务端的脚本)提供更多的参数.和通常的作法一样是使用一个键值对对象.如果传过去的值是{ bar:4 },将会被autocompleter解析成my_autocomplete_backend.php?q=foo&bar=4 (假设当前用户输入了foo). Default: {}

* result (handler) Returns: jQuery

    此事件会在用户选中某一项后触发,参数为:

    event: 事件对象. event.type为result.

    data: 选中的数据行.

    formatted:formatResult函数返回的值

    例如:

    $("#singleBirdRemote").result(function(event, data, formatted) {

//如选择后给其他控件赋值,触发别的事件等等

});

四、注意问题:

1.网上有人说对中文的检索时处理有问题,经过测试此版本没有问题^-^

2.在使用远程地址时,它默认传入的参数是:q(输入值),limit(返回结果的最大值),可以使用extraParams传入其他的参数

3.autocomplete在使用ajax传递参数时,默认使用了get方式传递,也实在是没有找到可以通过参数提交post方式的办法。

解决方式1:在使用ajax的get方式传递中文时,使用new String(request.getParameter("q").getBytes("iso8859-1"),"utf-8")获得参数值

解决方式2:修改jquery.autocomplete.js代码,把get方式修改为post方式,然后参见之前ajax解决中文乱码的问题的

 
 
 

autoFill

Boolean

Default: false

Fill the textinput while still selecting a value, replacing the value if more is typed or something else is selected.

cacheLength

Number

Default: 10

The number of backend query results to store in cache. If set to 1 (the current result), no caching will happen. Must be >= 1.

delay

Number

Default: 400 for remote, 10 for local

The delay in milliseconds the autocompleter waits after a keystroke to activate itself.

extraParams

Object

 

Extra parameters for the backend. If you were to specify { bar:4 }, the autocompleter would call my_autocomplete_backend.php?q=foo&bar=4 (assuming the input box contains "foo"). The param can be a function that is called to calculate the param before each request.

formatItem

Function

Default: Assumes that a single row contains a single value.

Provides advanced markup for an item. For each row of results, this function will be called. The returned value will be displayed inside an LI element in the results list. Autocompleter will provide 4 parameters: the results row, the position of the row in the list of results (starting at 1), the number of items in the list of results and the search term.

formatMatch

Function

Default: formatItem is used

Use this option if you want to limit the data that autocomplete searches for matches. For example, there may be items you want displayed to the user, but don't want included in the data that's searched. Gets called with the same arguments as formatItem.

formatResult

Function

Default: Assumes either plain data to use as result or uses the same value as provided by formatItem.

Similar to formatItem, but provides the formatting for the value to be put into the input field. Again three arguments: Data, position (starting with one) and total number of data.

highlight

Boolean, Function

Default: Wraps the search term in a <strong>Element

Whether and how to highlight matches in the select box. Set to false to disable. Set to a function to customize. The function gets the value as the first argument and the search term as the second and must return the formatted value.

matchCase

Boolean

Default: false

Whether or not the comparison is case sensitive. Important only if you use caching.

matchContains

Boolean

Default: false

Whether or not the comparison looks inside (i.e. does "ba" match "foo bar") the search results. Important only if you use caching. Don't mix with autofill.

matchSubset

Boolean

Default: true

Whether or not the autocompleter can use a cache for more specific queries. This means that all matches of "foot" are a subset of all matches for "foo". Usually this is true, and using this options decreases server load and increases performance. Only useful with cacheLength settings bigger than one, like 10.

max

Number

Default: 10

Limit the number of items in the select box. Is also sent as a "limit" parameter with a remote request.

minChars

Number

Default: 1

The minimum number of characters a user has to type before the autocompleter activates.

multiple

Boolean

Default: false

Whether to allow more than one autocompleted-value to enter.

multipleSeparator

String

Default: ", "

Seperator to put between values when using multiple option.

mustMatch

Boolean

Default: false

If set to true, the autocompleter will only allow results that are presented by the backend. Note that illegal values result in an empty input box.

scroll

Boolean

Default: true

Whether to scroll when more results than configured via scrollHeight are available.

scrollHeight

Number

Default: 180

height of scrolled autocomplete control in pixels

selectFirst

Boolean

Default: true

If this is set to true, the first autocomplete value will be automatically selected on tab/return, even if it has not been handpicked by keyboard or mouse action. If there is a handpicked (highlighted) result, that result will take precedence.

width

Number

Default: width of the input Element

Specify a custom width for the select box.

 

end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值