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

Jquery AutoComplete自動完成 的使用方法實例

 更新時間:2010年03月19日 12:00:57   作者:  
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(自動完成)代碼片段:
復制代碼 代碼如下:

<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插件里的重要方法,它在用戶在選定了某個條目時觸發(fā)。data參數(shù)為選中的數(shù)據(jù)。

一個稍微復雜的例子,可以自定義提示列表:
復制代碼 代碼如下:

<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, //列表里的條目數(shù)
minChars: 0, //自動完成激活之前填入的最小字符
width: 400, //提示的寬度,溢出隱藏
scrollHeight: 300, //提示的高度,溢出顯示滾動條
matchContains: true, //包含匹配,就是data參數(shù)里的數(shù)據(jù),是否只要包含文本框里的數(shù)據(jù)就顯示
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是自定提示信息的關(guān)鍵。
formatItem作用在于可以格式化列表中的條目,比如我們加了“I”,讓列表里的字顯示出了斜體。
formatMatch是配合formatItem使用,作用在于,由于使用了formatItem,所以條目中的內(nèi)容有所改變,而我們要匹配的是原始的數(shù)據(jù),所以用formatMatch做一個調(diào)整,使之匹配原始數(shù)據(jù),
formatResult是定義最終返回的數(shù)據(jù),比如我們還是要返回原始數(shù)據(jù),而不是formatItem過的數(shù)據(jù)。

jquery bassistance.de AutoComplete自動完成效果代碼下載

相關(guān)文章

最新評論