不出现提前建议

时间:2014-08-11 15:59:02

标签: typeahead.js

大家好,我正在尝试使用一个小项目和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">

1 个答案:

答案 0 :(得分:0)

使用时

jQuery.noConflict();

你不能再使用$了,如

$.each(strs, function(i, str) {

该行应为

jQuery.each(strs, function(i, str) {

这解决了这个问题。看看jsbin:

http://jsbin.com/ratuho/1/