Jquery AutoComplete自動完成 的使用方法實(shí)例
更新時間:2010年03月19日 12:00:57 作者:
jQuery的Autocomplete(自動完成、自動填充)插件有不少,但比較下來我感覺,還是bassistance.de的JQuery Autocomplete plugin比較強(qiáng)大,我們就來寫一些代碼感受一下。
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ù)。
一個稍微復(fù)雜的例子,可以自定義提示列表:
<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自動完成效果代碼下載
<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(自動完成)代碼片段:
復(fù)制代碼 代碼如下:
<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ù)。
一個稍微復(fù)雜的例子,可以自定義提示列表:
復(fù)制代碼 代碼如下:
<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自動完成效果代碼下載
您可能感興趣的文章:
- jQuery.Autocomplete實(shí)現(xiàn)自動完成功能(詳解)
- jQuery UI AutoComplete 使用說明
- jQuery UI Autocomplete 體驗(yàn)分享
- jQuery UI AutoComplete 自動完成使用小記
- jQuery Autocomplete自動完成插件
- JQuery autocomplete 使用手冊
- 基于jquery的文本框與autocomplete結(jié)合使用(asp.net+json)
- jQuery autocomplete插件修改
- jQuery Autocomplete簡介_動力節(jié)點(diǎn)Java學(xué)院整理
相關(guān)文章
Javascript函數(shù)中的arguments.callee用法實(shí)例分析
這篇文章主要介紹了Javascript函數(shù)中的arguments.callee用法,結(jié)合實(shí)例形式分析了arguments.callee操作當(dāng)前方法引用的具體技巧,需要的朋友可以參考下2016-09-09
淺析jQuery中調(diào)用ajax方法時在不同瀏覽器中遇到的問題
這篇文章主要介紹了jQuery中調(diào)用ajax方法時在不同瀏覽器中遇到的問題,因不同瀏覽器默認(rèn)設(shè)置的不同造成的問題2014-06-06
使用jQuery的toggle()方法對HTML標(biāo)簽進(jìn)行顯示、隱藏的方法(示例)
本文通過示例給大家介紹了使用jQuery的toggle()方法對HTML標(biāo)簽進(jìn)行顯示、隱藏操作的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
jQuery+ajax實(shí)現(xiàn)文章點(diǎn)贊功能的方法
這篇文章主要介紹了jQuery+ajax實(shí)現(xiàn)文章點(diǎn)贊功能的方法,涉及jQuery基于ajax無刷新post提交實(shí)現(xiàn)點(diǎn)贊功能的具體步驟與相關(guān)技巧,需要的朋友可以參考下2015-12-12
slideToggle+slideup實(shí)現(xiàn)手機(jī)端折疊菜單效果
這篇文章主要為大家詳細(xì)介紹了slideToggle+slideup實(shí)現(xiàn)手機(jī)端折疊菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
jquery showModelDialog的使用方法示例詳解
這篇文章主要介紹了window.showModalDialog的使用方法,大家要以參考使用2013-11-11
Jquery屬性的獲取/設(shè)置及樣式添加/刪除操作技巧分析
這篇文章主要介紹了Jquery屬性的獲取/設(shè)置及樣式添加/刪除操作技巧,結(jié)合實(shí)例形式分析了jquery針對屬性與樣式的相關(guān)獲取、設(shè)置、添加、刪除等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-12-12
jquery中g(shù)et,post和ajax方法的使用小結(jié)
本篇文章主要是對jquery中g(shù)et,post和ajax方法的使用進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02

