【发布时间】:2014-05-30 10:08:01
【问题描述】:
在我的 mvc 项目中,我试图实现自动完成但它不起作用(打字机)我做的一切都是正确的,但无法得到它。下面是我的代码。谁能帮忙
<script type="text/javascript">
$(document).ready(function () {
$("#Search").typeahead({
source: function (query, process) {
var countries = [];
map = {};
// This is going to make an HTTP post request to the controller
return $.post('/Registration/GetPossibleLocations', { query: query }, function (data) {
// Loop through and push to the array
$.each(data, function (i, country) {
map[country.Name] = country;
countries.push(country.Name);
});
// Process the details
process(countries);
});
},
updater: function (item) {
var selectedShortCode = map[item].ShortCode;
// Set the text to our selected id
$("#details").text("Selected : " + selectedShortCode);
return item;
}
});
});
<script src="http://code.jquery.com/jquery-1.8.3.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js" type="text/javascript"> </script>
<script src="http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js" type="text/javascript"></script>
<div>
<input type="text" id="Search" data-provide="typeahead" placeholder="Country" autocomplete="off" />
</div>
【问题讨论】:
-
你能指定什么不工作是什么意思吗?您收到 ajax 调用的响应了吗?您的
countries变量中是否有任何元素? -
甚至没有预先输入调用控制器它卡住什么也没发生没有错误没有数据
-
typeahed.js Github site 上有一条通知:注意:bloodhound.js 和 typeahead.jquery.js 都依赖于 jQuery 1.9+。您使用的是 1.8.3。也许这就是问题所在。
-
即使更改为 1.10 也无法正常工作
标签: javascript jquery asp.net-mvc twitter-bootstrap-3 bootstrap-typeahead