JQuery getting started tutorial (25): jQuery UI Autocomplete example (3)

Source: Internet
Author: User
Tags jquery ui autocomplete

When you enter AutoComplete, you can give a prompt based on the user input. The data source can be local or remote. The DataSource of AutoComplete can use a Function. In this article, we use JSONP to query the Function.
When the data source is Function, its Function is defined:
Function (Object request, Function response (Object data ))
Request is a request that contains a term attribute, Which is input by the current user.
Response is a callback function, which has a parameter data to indicate the user's data. This value should be filtered according to the input term.
This example uses the Web Service provided by genames. You can query the name of a city in the world. Use the. ajax function provided by jQuery to access this Web Service.

1 $ ("# city"). autocomplete ({

2 source: function (request, response ){

3 $. ajax ({

4 url: "http://ws.geonames.org/searchJSON ",

5 dataType: "jsonp ",

6 data :{

7 featureClass: "P ",

8 style: "full ",

9 maxRows: 12,

10 name_startsWith: request. term

11 },

12 success: function (data ){

13 response ($. map (data. geonames, function (item ){

14 return {

15 label: item. name + (item. adminName1? ","

16 + item. adminName1: "") + "," + item. countryName,

17 value: item. name

18 };

19 }));

20}

21 });

22 },

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.