jQuery的Autocomplete實現自動完成/自動填充

jQuery的Autocomplete(自動完成、自動填充)插件有不少,但比較下來我感覺,還是bassistance.de的JQuery Autocomplete plugin比較強大,我們就來寫一些代碼感受一下。

 

jquery-autocomplete配置:

<script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
 <script type="text/javascript" src="/js/jquery.autocomplete.min.js"></script>
 <link rel="Stylesheet" href="/js/jquery.autocomplete.css" />

 

首先是一個最簡單的Autocomplete(自動完成)代碼片段:

 

複製代碼
 1 <html xmlns="http://www.w3.org/1999/xhtml">
 2 <head runat="server">
 3     <title>AutoComplate</title>
 4     <script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
 5     <script type="text/javascript" src="/js/jquery.autocomplete.min.js"></script>
 6     <link rel="Stylesheet" href="/js/jquery.autocomplete.css" />
 7     <script type="text/javascript">
 8         $(function() {
 9             var data = "Core Selectors Attributes Traversing Manipulation CSS Events Effects Ajax Utilities".split(" ");
10 
11             $('#keyword').autocomplete(data).result(function(event, data, formatted) {
12                 alert(data);
13             });
14         });
15     </script>
16 </head>
17 <body>
18     <form id="form1" runat="server">
19     <div>
20         <input id="keyword" />
21         <input id="getValue" value="GetValue" type="button" />
22     </div>
23     </form>
24 </body>
25 </html>
複製代碼

 

result方法是jQuery Autocomplete插件裏的重要方法,它在用戶在選定了某個條目時觸發。data參數爲選中的數據。

 

一個稍微複雜的例子,可以自定義提示列表:

複製代碼
 1 <html xmlns="http://www.w3.org/1999/xhtml">
 2 <head runat="server">
 3     <title>自定義提示</title>
 4     <script type="text/javascript" src="/js/jquery-1.4.2.min.js"></script>
 5     <script type="text/javascript" src="/js/jquery.autocomplete.min.js"></script>
 6     <link rel="Stylesheet" href="/js/jquery.autocomplete.css" />
 7     <script type="text/javascript">
 8         var emails = [
 9             { name: "Peter Pan", to: "[email protected]" },
10             { name: "Molly", to: "[email protected]" },
11             { name: "Forneria Marconi", to: "[email protected]" },
12             { name: "Master <em>Sync</em>", to: "[email protected]" },
13             { name: "Dr. <strong>Tech</strong> de Log", to: "[email protected]" },
14             { name: "Don Corleone", to: "[email protected]" },
15             { name: "Mc Chick", to: "[email protected]" },
16             { name: "Donnie Darko", to: "[email protected]" },
17             { name: "Quake The Net", to: "[email protected]" },
18             { name: "Dr. Write", to: "[email protected]" },
19             { name: "GG Bond", to: "[email protected]" },
20             { name: "Zhuzhu Xia", to: "[email protected]" }
21         ];
22 
23             $(function() {
24                 $('#keyword').autocomplete(emails, {
25                     max: 12,    //列表裏的條目數
26                     minChars: 0,    //自動完成激活之前填入的最小字符
27                     width: 400,     //提示的寬度,溢出隱藏
28                     scrollHeight: 300,   //提示的高度,溢出顯示滾動條
29                     matchContains: true,    //包含匹配,就是data參數裏的數據,是否只要包含文本框裏的數據就顯示
30                     autoFill: false,    //自動填充
31                     formatItem: function(row, i, max) {
32                         return i + '/' + max + ':"' + row.name + '"[' + row.to + ']';
33                     },
34                     formatMatch: function(row, i, max) {
35                         return row.name + row.to;
36                     },
37                     formatResult: function(row) {
38                         return row.to;
39                     }
40                 }).result(function(event, row, formatted) {
41                     alert(row.to);
42                 });
43             });
44     </script>
45 </head>
46 <body>
47     <form id="form1" runat="server">
48     <div>
49         <input id="keyword" />
50         <input id="getValue" value="GetValue" type="button" />
51     </div>
52     </form>
53 </body>
54 </html>
複製代碼

 

formatItem、formatMatch、formatResult是自定提示信息的關鍵。

formatItem作用在於可以格式化列表中的條目,比如我們加了“I”,讓列表裏的字顯示出了斜體。

formatMatch是配合formatItem使用,作用在於,由於使用了formatItem,所以條目中的內容有所改變,而我們要匹配的是原始的數據,所以用formatMatch做一個調整,使之匹配原始數據,

formatResult是定義最終返回的數據,比如我們還是要返回原始數據,而不是formatItem過的數據。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章