jquery UI實(shí)現(xiàn)autocomplete在獲取焦點(diǎn)時得到顯示列表功能示例
本文實(shí)例講述了jquery UI實(shí)現(xiàn)autocomplete在獲取焦點(diǎn)時得到顯示列表功能。分享給大家供大家參考,具體如下:
在做項(xiàng)目的時候,客戶有這樣的需求,將以前輸入過的內(nèi)容,在某個文本框上用列表的形式提示出來,可以選擇,換言之,就如同我們用谷歌搜索,或者百度搜索一樣,輸入一些關(guān)鍵詞,會自動提示,這個功能就叫autocomplete. 當(dāng)然在 jquery UI 下有 插件,具體下載的地方,搜索就知道了。重點(diǎn)是,我現(xiàn)在的用法,是需要在文本框獲取焦點(diǎn)的時候,就彈出待選擇的列表。而傳統(tǒng)的是必須在輸入文字之后才出現(xiàn)。經(jīng)過發(fā)現(xiàn),jquery ui autocomplete 用如下方法可以實(shí)現(xiàn)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Categories</title>
<link rel="stylesheet" href="../../themes/base/jquery.ui.all.css" rel="external nofollow" >
<script src="../../jquery-1.9.1.js"></script>
<script src="../../ui/jquery.ui.core.js"></script>
<script src="../../ui/jquery.ui.widget.js"></script>
<script src="../../ui/jquery.ui.position.js"></script>
<script src="../../ui/jquery.ui.menu.js"></script>
<script src="../../ui/jquery.ui.autocomplete.js"></script>
<link rel="stylesheet" href="../demos.css" rel="external nofollow" >
<style>
.ui-autocomplete-category {
font-weight: bold;
padding: .2em .4em;
margin: .8em 0 .2em;
line-height: 1.5;
}
</style>
<script>
var data = [
{ label: "anders", category: "" },
{ label: "andreas", category: "" },
{ label: "antal", category: "" },
{ label: "annhhx10", category: "Products" },
{ label: "annk K12", category: "Products" },
{ label: "annttop C13", category: "Products" },
{ label: "anders andersson", category: "People" },
{ label: "andreas andersson", category: "People" },
{ label: "andreas johnson", category: "People" }
];
function dynamicAutocomplete(){
$("#search").autocomplete({
delay:200,
autoFocus: false,
source: data,
minLength: 0,
}).focus(function () {
$(this).autocomplete("search");
});
}
</script>
</head>
<body>
<button onclick="dynamicAutocomplete()">autocomplete</button> <br />
<label for="search">Search: </label>
<input id="search">
<div class="demo-description">
<p>A categorized search result. Try typing "a" or "n".</p>
</div>
</body>
</html>
代碼來源于官網(wǎng)例子,稍稍改動了一下,但貌似在IE 下有點(diǎn)問題,選擇某個選項(xiàng)之后,這個列表框不消失,還一直存在,比較郁悶。
在google 上搜索了一下,發(fā)現(xiàn)了一篇文章,也講到了這個問題。后來用如下方法解決,不過是失去焦點(diǎn)的方式做的。
function dynamicAutocomplete(){
$("#search").autocomplete({
minLength: 0,
source: data,
focus :function () {
return false;
},
select: function(event, ui){
$this = $(this);
setTimeout(function () {
$this.blur();
}, 1);
}
}).focus(function(){
$(this).autocomplete("search");
return false;
}
);
};
在ie 下面用了timeout 來解決,在網(wǎng)上看到很多人說,在focus 方法中 return false 就可以解決,但我就是沒有測試成功.
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery頁面元素操作技巧匯總》、《jQuery常見事件用法與技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。
- jQuery UI AutoComplete 使用說明
- jQuery UI Autocomplete 體驗(yàn)分享
- jQuery UI AutoComplete 自動完成使用小記
- jQuery UI Autocomplete 1.8.16 中文輸入修正代碼
- jQuery ui autocomplete選擇列表被Bootstrap模態(tài)窗遮擋的完美解決方法
- firefox下jQuery UI Autocomplete 1.8.*中文輸入修正方法
- jQuery.Autocomplete實(shí)現(xiàn)自動完成功能(詳解)
- jQuery 插件autocomplete自動完成應(yīng)用(自動補(bǔ)全)(asp.net后臺)
- jquery autocomplete自動完成插件的的使用方法
- jQuery autoComplete插件兩種使用方式及動態(tài)改變參數(shù)值的方法詳解
- jQuery Autocomplete自動完成插件
相關(guān)文章
輕松學(xué)習(xí)jQuery插件EasyUI EasyUI實(shí)現(xiàn)拖動基本操作
這篇文章主要幫大家輕松學(xué)習(xí)jQuery插件EasyUI,并利用EasyUI實(shí)現(xiàn)拖動基本操作,文章并提供了一個學(xué)校課程表簡單實(shí)例,感興趣的小伙伴們可以參考一下2015-11-11
element form 校驗(yàn)數(shù)組每一項(xiàng)實(shí)例代碼
這篇文章主要介紹了element form 校驗(yàn)數(shù)組每一項(xiàng)的實(shí)例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
jquery 層次選擇器siblings與nextAll的區(qū)別介紹
jquery 層次選擇器包括siblings與nextAll,本文為大家介紹下具體的使用方法,想學(xué)習(xí)的朋也可以參考下,希望對大家有所幫助2013-08-08
多種方法實(shí)現(xiàn)360瀏覽器下禁止自動填寫用戶名密碼
這篇文章主要介紹了多種方法實(shí)現(xiàn)360瀏覽器下禁止自動填寫用戶名密碼,需要的朋友可以參考下2014-06-06
bootstrap table sum總數(shù)量統(tǒng)計(jì)實(shí)現(xiàn)方法
這篇文章主要介紹了bootstrap table sum總數(shù)量統(tǒng)計(jì)實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10
各瀏覽器中querySelector和querySelectorAll的實(shí)現(xiàn)差異分析
querySelector和querySelectorAll的參數(shù)須是符合 css selector 的字符串。不同的是querySelector返回的是一個對象,querySelectorAll返回的一個集合(NodeList)2012-05-05
基于jquery實(shí)現(xiàn)二級聯(lián)動效果
這篇文章主要為大家詳細(xì)介紹了基于jquery二級聯(lián)動效果的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03

