Bootstrap學(xué)習(xí)系列之使用 Bootstrap Typeahead 組件實(shí)現(xiàn)百度下拉效果
UnderScore官網(wǎng):http://underscorejs.org/
參考文檔:http://www.css88.com/doc/underscore/
頁面代碼:
@{
ViewBag.Title = "Index";
}
<script src="Scripts/bootstrap-typeahead.js"></script>
<script src="Scripts/underscore-min.js"></script>
<div>
簡(jiǎn)單使用
<div style="margin: 10px 50px"> <label for="product_search"> Product Search: </label> <input id="product_search" type="text" data-provide="typeahead" data-source='["DATA1", "DATA2", "DATA3"]' /> </div>
使用腳本填充數(shù)據(jù)
<div style="margin: 10px 50px">
<label for="product_search">
Product Search:
</label>
<input id="product_search_js" type="text" data-provide="typeahead">
</div>
<script type="text/javascript">
$(document).ready(function ($) {
$.fn.typeahead.Constructor.prototype.blur = function () {
var that = this;
setTimeout(function () { that.hide() }, 250);
};
$('#product_search_js').typeahead({
source: function (query, process) {
return ["JS數(shù)據(jù)1", "JS數(shù)據(jù)2", "JS數(shù)據(jù)3"];
},
highlighter: function (item) {
return "==>" + item + "<==";
},
updater: function (item) {
console.log("'" + item + "' selected."); //瀏覽器控制臺(tái)輸出
$("#product_search").val(item);
return item;
}
});
})
</script>
支持 Ajax 獲取數(shù)據(jù)
<div style="margin: 10px 50px">
<label for="product_search">
Product Search:
</label>
<input id="product_search_ajax" type="text" data-provide="typeahead">
</div>
<script type="text/javascript">
$('#product_search_ajax').typeahead({
source: function (query, process) {
var parameter = { query: query };
$.post('@Url.Action("AjaxService")', parameter, function (data) {
process(data);
});
}
});
</script>
使用對(duì)象數(shù)據(jù)
<div style="margin: 10px 50px">
<label for="product_search">
Product Search:
</label>
<input id="product_search_object" type="text" data-provide="typeahead">
</div>
<script type="text/javascript">
$(function () {
var products = [
{
id: 0,
name: "object1",
price: 499.98
},
{
id: 1,
name: "object2",
price: 134.99
},
{
id: 2,
name: "object3",
price: 49.95
}
];
$('#product_search_object').typeahead({
source: function (query, process) {
var results = _.map(products, function (product) {
return product.name;
});
process(results);
},
highlighter: function (item) {
return "==>" + item + "<==";
},
updater: function (item) {
console.log("'" + item + "' selected.");
return item;
}
});
});
</script>
</div>
控制器
public ActionResult Index()
{
return View();
}
public ActionResult AjaxService()
{
string query = "";
if (!string.IsNullOrWhiteSpace(Request["Query"]))
query = Request["Query"].ToString();
var data = ("AJAX1,AJAX2,AJAX3").Split(',');
return Json(data);
}
效果圖展示如下:

下面繼續(xù)給大家介紹BootStrap學(xué)習(xí)系列之Bootstrap Typeahead 組件實(shí)現(xiàn)百度下拉效果(續(xù))
以上所述是小編給大家介紹的Bootstrap學(xué)習(xí)系列之使用 Bootstrap Typeahead 組件實(shí)現(xiàn)百度下拉效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
JavaScript 浮動(dòng)定位提示效果實(shí)現(xiàn)代碼
本來想做一個(gè)集合浮動(dòng)定位和鼠標(biāo)跟隨的tooltips效果,但發(fā)現(xiàn)定位和鼠標(biāo)跟隨在一些關(guān)鍵的地方還是不同的,還是分開來吧。2009-09-09
JavaScript設(shè)計(jì)模式之代理模式介紹
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之代理模式介紹,代理模式顧名思義就是用一個(gè)類來代替另一個(gè)類來執(zhí)行方法功能,需要的朋友可以參考下2014-12-12
詳解JavaScript數(shù)組過濾相同元素的5種方法
本篇文章主要介紹了詳解JavaScript數(shù)組過濾相同元素的5種方法,詳細(xì)的介紹了5種實(shí)用方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05
基于PHP pthreads實(shí)現(xiàn)多線程代碼實(shí)例
這篇文章主要介紹了基于PHP pthreads實(shí)現(xiàn)多線程代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
純?JS?實(shí)現(xiàn)的輕量化圖片編輯器實(shí)例詳解
這篇文章主要為大家介紹了純JS實(shí)現(xiàn)的輕量化圖片編輯器實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
用iframe實(shí)現(xiàn)不刷新整個(gè)頁面上傳圖片的實(shí)例
下面小編就為大家?guī)硪黄胕frame實(shí)現(xiàn)不刷新整個(gè)頁面上傳圖片的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
javascript和jQuery實(shí)現(xiàn)網(wǎng)頁實(shí)時(shí)聊天的ajax長(zhǎng)輪詢
在做網(wǎng)頁實(shí)時(shí)聊天的時(shí)候常常需要長(zhǎng)輪詢,本文由于采用原生的JS及AJAX,所以簡(jiǎn)單易懂,通過這篇文章就可以建立一個(gè)簡(jiǎn)單的聊天室程序。2016-07-07

