【问题标题】:typeahead suggestions do not appear不出现预输入建议
【发布时间】: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


【解决方案1】:

当你使用

jQuery.noConflict();

你不能再使用$,就像在

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

那一行应该是

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

这解决了问题。看看jsbin:

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

【讨论】:

  • 你看过jsbin了吗?它在那里工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多