【发布时间】:2015-02-25 01:35:25
【问题描述】:
问题
我无法为我的 MVC 5 应用程序实现 twitter typeahead。我可以输入文本,但没有提前输入或自动完成功能。
- 已安装 twitter typeahead api
- 实现了本地 javascript
更新
我尝试在fiddle 上实现"working" 示例。我确信如果我可以让这些示例正常工作,那么我的应用程序也可以吗?
尝试
我已经检查了 twitter api。我也看过几个教程。 见下文:
教程 2 包含的小提琴代码也不起作用,其行为与我的 MVC 应用程序类似。 See here for fiddle code。
代码
我的整个应用程序都有自己的存储库。你可以找到它here。我在这里包括我的独立视图。我确实使用共享布局来放入必要的 javascript。
我不确定为什么这不起作用(代码也来自教程),但非常感谢您的帮助。我无法发现任何明显的语法错误。
@{
ViewBag.Title = "TypeAhead";
}
<script>
var substringMatcher = function (strs) {
return function findMatches(q, cb) {
var matches, substrRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function (i, str) {
if (substrRegex.test(str)) {
// the typeahead jQuery plugin expects suggestions to a
// JavaScript object, refer to typeahead docs for more info
matches.push({ value: str });
}
});
cb(matches);
};
};
var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];
$('#the-basics .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
displayKey: 'value',
source: substringMatcher(states)
});
</script>
<body>
<div id="the-basics">
<input class="typeahead" type="text" placeholder="States of USA">
</div>
</body>
【问题讨论】:
标签: javascript asp.net-mvc twitter-bootstrap typeahead.js