欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery實(shí)現(xiàn)用戶輸入自動(dòng)完成功能

 更新時(shí)間:2017年02月13日 17:06:45   作者:sumer7310  
本文介紹了jQuery實(shí)現(xiàn)用戶輸入自動(dòng)完成功能的方法。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧

利用jQuery UI中Auto-complete插件實(shí)現(xiàn)輸入自動(dòng)完成功能,大家在使用諸如淘寶、京東等電商平臺(tái)搜索商品時(shí),往往只要輸入商品的一些特殊字符,就可以顯示出和該字符相近的列表菜單,用戶使用鼠標(biāo)或者鍵盤方向鍵就可以快速選擇,實(shí)現(xiàn)了很好的用戶體驗(yàn)。

1.最簡(jiǎn)單的用戶輸入自動(dòng)完成

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>jQuery UI Autocomplete - Default functionality</title>
 <link rel="stylesheet"  rel="external nofollow" >
 <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
 <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
 <script>
 $(function() {
 //定義并初始化字典庫(kù)數(shù)據(jù)源集合
 var availableTags = [
 "ActionScript",
 "AppleScript",
 "Asp",
 "BASIC",
 "C",
 "C++",
 "Clojure",
 "COBOL",
 "ColdFusion",
 "Erlang",
 "Fortran",
 "Groovy",
 "Haskell",
 "Java",
 "JavaScript",
 "Lisp",
 "Perl",
 "PHP",
 "Python",
 "Ruby",
 "Scala",
 "Scheme"
 ];
 //自動(dòng)完成插件函數(shù)
 $( "#tags" ).autocomplete({
 //自動(dòng)完成字典庫(kù)數(shù)據(jù)源
 source: availableTags
 });
 });
 </script>
</head>
<body>
<div class="ui-widget">
 <label for="tags">請(qǐng)輸入: </label>
 <input id="tags">
</div>
</body>
</html>

2 使用遠(yuǎn)程數(shù)據(jù)源自動(dòng)完成

Auto-complete插件不光可以實(shí)現(xiàn)本地?cái)?shù)據(jù)源的自動(dòng)完成,也可以讀取遠(yuǎn)程的數(shù)據(jù)源,列如實(shí)現(xiàn)從服務(wù)器端讀取數(shù)據(jù)源信息。

通過(guò)將服務(wù)器數(shù)據(jù)緩存到瀏覽器中,獲取的數(shù)據(jù)源首先保存在cache變量中。

 $(function() {
 //自定義緩存變量
 var cache = {};
 //自動(dòng)完成插件函數(shù)
 $("#tags").autocomplete({
 //定義用戶最少輸入的字符數(shù)
 minLenght: 2,
 source: function(request, response){//定義遠(yuǎn)程獲取數(shù)據(jù)源函數(shù)
 var term = request.term;//定義用戶請(qǐng)求信息變量
 if(term in cache) {//判斷請(qǐng)求數(shù)據(jù)是否存在緩存中
 response(cache[term]);//真,從緩存中讀取數(shù)據(jù)
 return;
 }
 $.getJSON('data.json', request, function(data, Status, xhr) {
 cache[term] = data.result;//緩存遠(yuǎn)程數(shù)據(jù)
 response(data.result);
 });
 }
 });
 });

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

最新評(píng)論