我就不翻译了……
这些东西也不复杂。
Copy网站开始……黑黑
Dependencies
- jQuery
- bgiframe plugin
- dimensions plugin, only offset method is used
Known issues:
- When completing multiple values, only the last value can be edited with the autocomplete help
- mustMatch option is broken (mostly fixed in latest revision)
- default browser autocomplete displays a long list of values scrollable, that isn’t supported yet
Recent changes
- Heavily improved Dylan Verheul’s initial plugin, integrating modifications by Dan G. Switzer and Anjesh Tuladhar
Documentation
Autocompletition of input elements is a very helpful tool to guide users in entering the right value. That value can come from a (big) list of possible values or it may consist of values the user has entered before. This plugin makes it easy for a developer to setup autocomplete for text inputs and textareas.
Setup
First step in setting up autocomplete is to decide what kind of data is provided. To autocomplete a few dozen possible values, it is a good idea to provide local data. The data is loaded only once and then cached, providing a very responsive interface.
If the data can consist of hundreds, thousands or even millions of rows, the searching and filtering must be done where performance won’t suffer, in most cases a relational database (eg. MySQL). The plugin will request the rows for the entered value, displaying only a subset of all possible values.
For local autocomplete, just pass the data as an array to the autocomplete plugin. For remote autocomplete, specify an URL pointing to the resource providing the data. The plugin then requests data with a “q” parameter containing the current search value.
API
- autocomplete( String|Array urlOrData, Map options ) returns jQuery
Provide autocomplete for text-inputs or textareas.
Depends on dimensions plugin’s offset method for correct positioning of the select box and bgiframe plugin
to fix IE’s problem with selects.Options
Example:
Autocomplete a text-input with remote data. For small to giant datasets.
When the user starts typing, a request is send to the specified backend (”my_autocomplete_backend.php”),
with a GET parameter named q that contains the current value of the input box and a paremeter “limit” with
the value specified for the max option.A value of “foo” would result in this request url: my_autocomplete_backend.php?q=foo&limit=10
The result must return with one value on each line. The result is presented in the order
the backend sends it.$("#input_box").autocomplete("my_autocomplete_backend.php");
HTML:
<input id="input_box" />
Example:
Autcomplete a text-input with local data. For small datasets.
$("#input_box").autocomplete(["Cologne", "Berlin", "Munich"]);
HTML:
<input id="input_box" />
Example:
Autcomplete a text-input with data received via AJAX. For small to medium sized datasets.
$.getJSON("my_backend.php", function(data) { $("#input_box").autocomplete(data); });
HTML:
<input id="input_box" />
Example:
Autcomplete a textarea with local data (for small datasets). Once the user chooses one
value, a separator is appended (by default a comma, see multipleSeparator option) and more values
are autocompleted.$("#mytextarea").autocomplete(["Cologne", "Berlin", "Munich"], { multiple: true });
HTML:
<textarea id="mytextarea" />
- result( Function handler ) returns jQuery
Handle the result of a search event. Is executed when the user selects a value or a
programmatic search event is triggered (see search()).
You can add and remove (using unbind(”result”)) this event at any time.Example:
Bind a handler to the result event to display the selected value in a #result element.
The first argument is a generic event object, in this case with type “result”.
The second argument refers to the selected data, which can be a plain string value or an array or object.
The third argument is the formatted value that is inserted into the input field.jQuery('input#suggest').result(function(event, data, formatted) { jQuery("#result").html( !data ? "No match!" : "Selected: " + formatted); });
- search( ) returns jQuery
Trigger a search event. See result(Function) for binding to that event.
A search event mimics the same behaviour as when the user selects a value from
the list of autocomplete items. You can use it to execute anything that does something
with the selected value, beyond simply putting the value into the input and submitting it.Example:
Triggers a search event.
jQuery('input#suggest').search();