ASP.NET?MVC使用typeahead.js實(shí)現(xiàn)輸入智能提示功能
使用typeahead.js可以實(shí)現(xiàn)預(yù)先輸入,即智能提示,本篇在ASP.NET MVC下實(shí)現(xiàn)。實(shí)現(xiàn)效果如下:

首先是有關(guān)城市的模型。
public class City
{
public int Id { get; set; }
public string Name { get; set; }
public string PinYin { get; set; }
}在HomeController中響應(yīng)前端請(qǐng)求返回有關(guān)City的json數(shù)據(jù)。
public ActionResult GetCitiesJson()
{
var result = new List<City>()
{
new City(){Id = 1, Name = "青島",PinYin = "qingdao"},
new City(){Id = 10, Name = "青山",PinYin = "qingshan"},
new City(){Id = 11, Name = "青峰",PinYin = "qingfeng"},
new City(){Id = 2, Name = "武漢",PinYin = "wuhan"},
new City(){Id = 3, Name = "煙臺(tái)",PinYin = "yantai"},
new City(){Id = 4, Name = "哈爾濱",PinYin = "haerbing"},
new City(){Id = 5, Name = "北京",PinYin = "beijing"},
new City(){Id = 6, Name = "安陽(yáng)",PinYin = "angyang"},
new City(){Id = 7, Name = "長(zhǎng)春",PinYin = "changchun"},
new City(){Id = 8, Name = "東陽(yáng)",PinYin = "dongyang"},
new City(){Id = 9, Name = "葛洲壩",PinYin = "gezhoubei"}
};
return Json(result,JsonRequestBehavior.AllowGet);
}在視圖中先加載City集合,再使用預(yù)先輸入功能。
@section styles
{
<link href="~/Content/TypeHead.css" rel="external nofollow" rel="stylesheet" />
}
<div style="margin: 50px;">
<input class="typeahead" type="text" placeholder="輸入城市名稱">
</div>
@section scripts
{
<script src="~/Scripts/typeahead.bundle.js"></script>
<script type="text/javascript">
$(function () {
$.getJSON('@Url.Action("GetCitiesJson","Home")', function(data) {
if (data) {
$.each(data, function(index, city) {
cities.push(city.Name);
});
}
});
//預(yù)先輸入功能
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'city',
displayKey: 'value',
source: substringMatcher(cities)
});
});
var cities = [];
//參數(shù)arr表示數(shù)據(jù)源 數(shù)組
var substringMatcher = function (arr) {
return function findMatches(q, cb) {
var substrRegex;
var matches = [];
substrRegex = new RegExp(q, 'i');
$.each(arr, function (i, ele) {
if (substrRegex.test(ele)) {
matches.push({ value: ele });
}
});
cb(matches);
};
};
</script>
}以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- Vue實(shí)現(xiàn)typeahead組件功能(非常靠譜)
- BootStrap Typeahead自動(dòng)補(bǔ)全插件實(shí)例代碼
- Bootstrap學(xué)習(xí)系列之使用 Bootstrap Typeahead 組件實(shí)現(xiàn)百度下拉效果
- BootStrap學(xué)習(xí)系列之Bootstrap Typeahead 組件實(shí)現(xiàn)百度下拉效果(續(xù))
- Bootstrap3使用typeahead插件實(shí)現(xiàn)自動(dòng)補(bǔ)全功能
- 使用bootstrap typeahead插件實(shí)現(xiàn)輸入框自動(dòng)補(bǔ)全之問(wèn)題及解決辦法
- 使用Bootstrap typeahead插件實(shí)現(xiàn)搜索框自動(dòng)補(bǔ)全的方法
相關(guān)文章
asp.net用Zxing庫(kù)實(shí)現(xiàn)條形碼輸出的具體實(shí)現(xiàn)
這篇文章主要介紹了asp.net用Zxing庫(kù)實(shí)現(xiàn)條形碼輸出的具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-12-12
asp.net(C#) 動(dòng)態(tài)添加非ASP的標(biāo)準(zhǔn)html控件(如添加Script標(biāo)簽)
在開(kāi)發(fā)程序時(shí),有時(shí)需要?jiǎng)討B(tài)添加標(biāo)簽,而有部分又不是ASP控件,偶然找到這段代碼,特收藏。2009-07-07
Web.config 和 App.config 的區(qū)別分析
Web.config 和 App.config 的區(qū)別分析,需要的朋友可以參考一下2013-05-05
GridView分頁(yè)代碼簡(jiǎn)單萬(wàn)能實(shí)用
GridView在使用.net技術(shù)搭建的后臺(tái),在商品列表或者是信息列表經(jīng)常會(huì)出現(xiàn);它的作用在于有效的管理信息,增刪改查等等最主要的是還可以實(shí)現(xiàn)分頁(yè),這一點(diǎn)是無(wú)可比靡的,接下來(lái)介紹如何使用GridView實(shí)現(xiàn)分頁(yè),需要了解的朋友可以參考下2012-12-12
ASP.NET調(diào)用WebService服務(wù)的方法詳解
這篇文章主要介紹了ASP.NET調(diào)用WebService服務(wù)的方法,較為詳細(xì)的分析了WebService服務(wù)的功能,創(chuàng)建步驟與使用方法,需要的朋友可以參考下2016-05-05

