【问题标题】:basic typeahead.js not working?基本 typeahead.js 不起作用?
【发布时间】:2015-07-15 19:14:21
【问题描述】:

在研究和编辑代码几个小时后,我一直在问这个问题,我想实现 typehead.js 来实现自动完成,但它不起作用,如果有人能帮助我解决这个问题,我将非常感激......

<div class="form-group">
            <div class="input-group">
                <div id="the-basics">
    <input class="typeahead" type="text" placeholder="States of USA">
                </div>
                <script>

  $(document).ready(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)) {
        matches.push(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',
  source: substringMatcher(states)
});
}
</script>

                <!--<span class="input-group-addon info"><span class="glyphicon glyphicon-asterisk"></span></span>-->
            </div></div>

【问题讨论】:

  • 您遇到了什么错误?
  • 没有错误......只是没有建议值
  • 检查控制台。一定有错误。
  • 你能告诉我如何检查屏幕上没有显示错误
  • Uncaught SyntaxError: missing ) after argument list before

标签: jquery typeahead.js


【解决方案1】:

问题可能在于,如果您使用的是 Bootstrap 3.x.x,其中 typeahead 已被删除。

您可以使用 Twitter typeahead.js 库。有一篇关于它的不错的博文(最后也是关于简单集成的):https://blog.twitter.com/2013/twitter-typeaheadjs-you-autocomplete-me

更新

github 上还有一个项目,它在 Bootstrap 中提供了 back typeahead:https://github.com/bassjobsen/Bootstrap-3-Typeahead

【讨论】:

  • 这减少了控制台中的错误,但仍然无法自动完成
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
相关资源
最近更新 更多