Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery UI Examples - Autocomplete

jQuery UI Examples - Autocomplete

Searches and filters based on the value the user types, letting users quickly find and select from a list of preset values.

For more details about the autocomplete widget, see the API documentation Autocomplete Widget。

This section uses search.php download。

Default functionality

As you type in the input field, the Autocomplete widget provides suggestions. In this example, suggestions of programming languages are provided; try typing "ja" to get Java or JavaScript.

The data source is a simple JavaScript array provided to the widget via the source option.









  

  jQuery UI 自动完成(Autocomplete) - 默认功能

  

  

  

  

  





 

Accent folding

The autocomplete field uses a custom source option to match result items with accented characters even when the text field has no accents. However, if you type an accented character in the text field, non-accented result items will not be shown.

Try typing "Jo" to see "John" and "Jörn", then type "Jö" to see only "Jörn".









  

  jQuery UI 自动完成(Autocomplete) - 包含重音

  

  

  

  

  





 

Classification

Categorized search results. Try typing "a" or "n".









  

  jQuery UI 自动完成(Autocomplete) - 分类

  

  

  

  

  

  

  





 





 

 





Combobox

A custom widget created from Autocomplete and Button. You can type a few characters to get results filtered based on your input, or use the button to select from the full list.

The input is read from an existing select element and passed to Autocomplete with a custom source option.

This is an imperfect, unsupported widget. It is here purely to demonstrate autocomplete customization.For more details about how this widget works, click here to see the related jQuery article.









  

  jQuery UI 自动完成(Autocomplete) - 组合框(Combobox)

  

  

  

  

  

  





 

Custom data and display

You can use custom data formats and display the data by simply overriding the default focus and select behaviors.

Try typing "j" or pressing the down-arrow key to get a list of items.









  

  jQuery UI 自动完成(Autocomplete) - 自定义数据并显示

  

  

  

  

  

  





 

选择一个项目(请键入 "j"):

Multiple values

Usage: type some characters, such as "j", to see matching programming-language results. Select a value, then keep typing characters to add other values.

This example demonstrates how to use the source option and some events to enter multiple autocomplete values in a single text field.









  

  jQuery UI 自动完成(Autocomplete) - 多个值

  

  

  

  

  





 

Multiple values, remote

Usage: type at least two characters to get bird names. Select a value, then keep typing characters to add other values.

This example demonstrates how to use the source option and some events to enter multiple autocomplete values in a single text field.









  

  jQuery UI 自动完成(Autocomplete) - 多个值,远程

  

  

  

  

  

  





 

Remote JSONP data source

Usage: type at least two characters to get bird names. Select a value, then keep typing characters to add other values.

This example demonstrates how to use jsonp to fetch data.









  

  

  jQuery UI Autocomplete - Remote JSONP datasource

  

  

  

  

  

  





 

Result:

Remote data source

As you type characters in the text field, the Autocomplete widget gives suggestions. In this example, when you type at least two characters, the names of matching birds are displayed.

In this example, the data source is a server-side script returning JSON data, specified through a simple source option. In addition, the minLength option is set to 2 to avoid queries returning too many results, and the select event is used to display some feedback.









  

  jQuery UI 自动完成(Autocomplete) - 远程数据源

  

  

  

  

  

  





 

结果:

Remote cache

As you type characters in the text field, the Autocomplete widget gives suggestions. In this example, when you type at least two characters, the names of matching birds are displayed.

To improve performance, some local caching has been added here; otherwise it is similar to the remote data source example. Here the cache stores only one query, and can be extended to cache multiple values, one per entry.









  

  jQuery UI 自动完成(Autocomplete) - 远程缓存

  

  

  

  

  

  





 

Scrollable results

When displaying a long list of options, you can simply set max-height on the autocomplete menu to prevent it from becoming too tall. Try typing "a" or "s" to get a long scrollable list of results.









  

  jQuery UI 自动完成(Autocomplete) - 可滚动的结果

  

  

  

  

  

  





 

XML data

This example demonstrates how to fetch some XML data, parse it with jQuery's methods, and then provide it to autocomplete as a data source.

This example also serves as a reference for parsing a remote XML data source - parsing happens on every source callback request.









  

  jQuery UI 自动完成(Autocomplete) - XML 数据

  

  

  

  

  

  





 

结果: