基本的typeahead.js无法正常工作?

时间:2015-07-15 19:14:22

标签: jquery typeahead.js

在所有的研究和编辑代码几个小时之后,我一直在问这个问题,我想实现typehead.js来实现自动完成但是它不起作用我将非常感谢如果有人可以帮助我... < / p>

<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>

1 个答案:

答案 0 :(得分:0)

问题可能是如果您使用的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