DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> jQuery入門知識 >> JQuery特效代碼 >> Jquery AutoComplete自動完成 的使用方法實例
Jquery AutoComplete自動完成 的使用方法實例
編輯:JQuery特效代碼     
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(自動完成)代碼片段:
代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AutoComplate</title>
<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" />
<script type="text/javascript">
$(function() {
var data = "Core Selectors Attributes Traversing Manipulation CSS Events Effects Ajax Utilities".split(" ");

$('#keyword').autocomplete(data).result(function(event, data, formatted) {
alert(data);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="keyword" />
<input id="getValue" value="GetValue" type="button" />
</div>
</form>
</body>
</html>

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

一個稍微復雜的例子,可以自定義提示列表:
代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>自定義提示</title>
<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" />
<script type="text/javascript">
var emails = [
{ name: "Peter Pan", to: "peter@pan.de" },
{ name: "Molly", to: "molly@yahoo.com" },
{ name: "Forneria Marconi", to: "live@japan.jp" },
{ name: "Master <em>Sync</em>", to: "205bw@samsung.com" },
{ name: "Dr. <strong>Tech</strong> de Log", to: "g15@logitech.com" },
{ name: "Don Corleone", to: "don@vegas.com" },
{ name: "Mc Chick", to: "info@donalds.org" },
{ name: "Donnie Darko", to: "dd@timeshift.info" },
{ name: "Quake The Net", to: "webmaster@quakenet.org" },
{ name: "Dr. Write", to: "write@writable.com" },
{ name: "GG Bond", to: "Bond@qq.com" },
{ name: "Zhuzhu Xia", to: "zhuzhu@qq.com" }
];

$(function() {
$('#keyword').autocomplete(emails, {
max: 12, //列表裡的條目數
minChars: 0, //自動完成激活之前填入的最小字符
width: 400, //提示的寬度,溢出隱藏
scrollHeight: 300, //提示的高度,溢出顯示滾動條
matchContains: true, //包含匹配,就是data參數裡的數據,是否只要包含文本框裡的數據就顯示
autoFill: false, //自動填充
formatItem: function(row, i, max) {
return i + '/' + max + ':"' + row.name + '"[' + row.to + ']';
},
formatMatch: function(row, i, max) {
return row.name + row.to;
},
formatResult: function(row) {
return row.to;
}
}).result(function(event, row, formatted) {
alert(row.to);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="keyword" />
<input id="getValue" value="GetValue" type="button" />
</div>
</form>
</body>
</html>

formatItem、formatMatch、formatResult是自定提示信息的關鍵。
formatItem作用在於可以格式化列表中的條目,比如我們加了“I”,讓列表裡的字顯示出了斜體。
formatMatch是配合formatItem使用,作用在於,由於使用了formatItem,所以條目中的內容有所改變,而我們要匹配的是原始的數據,所以用formatMatch做一個調整,使之匹配原始數據,
formatResult是定義最終返回的數據,比如我們還是要返回原始數據,而不是formatItem過的數據。

jquery bassistance.de AutoComplete自動完成效果代碼下載
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved