【发布时间】:2014-08-11 15:59:01
【问题描述】:
大家好,我正在尝试使用一个小项目和我的 typeahead.js,但无论我做什么,它都无法正常工作,我已经重新编写了代码,删除了其他 javascript 库,但没有任何效果,有什么可以是吗?
我的 javascript
jQuery.noConflict();
jQuery(document).ready(function(){
jQuery(function(){
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// 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 colors = ["red", "blue", "green", "yellow", "brown", "black"];
jQuery('.typeahead').typeahead( {}, {source: substringMatcher(colors)} );
});
});
html
<input type="text" class="typeahead" data-provide="typeahead">
【问题讨论】:
-
发布您编写的代码 - 或发布它的 jsFiddle。
-
还要指定您正在使用的 typeahead.js 脚本...现在肯定有成千上万的此类脚本存在;-)
-
我正在使用 twitter typeahead.js
标签: typeahead.js