jQuery 入門教程(25): jQuery UI Autocomplete樣本(三)

來源:互聯網
上載者:User

AutoComplete在使用者輸入時,可以根據使用者輸入給出提示。其資料來源可以來自本地,也可以使用遠端資料源,AutoComplete 的DataSource可以使用一個Function,本篇中的Function,我們使用JSONP查詢。 
當資料來源為Function時,其函數定義為:
Function( Object request, Function response( Object data))
其中 request 為請求,它含有一個term屬性,為目前使用者輸入。
response 為一回呼函數,它有一個參數data,用來提示使用者的資料,這個值理應根據輸入的term進行過濾。
本例使用由genames提供的Web Service。可以查詢世界的城市名稱。使用了jQuery 提供的.ajax 函數來訪問這個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   },

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.