Bootstrap typeahead不起作用

时间:2014-08-19 13:38:45

标签: jquery html css twitter-bootstrap bootstrap-typeahead

有些人可以帮助弄清楚这段代码有什么问题吗,typeahead似乎不起作用。任何帮助都是非常明确的。

<html>
  <head>
    <link href="css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
    <div style="margin: 50px 50px">
      <label for="product_search">Product Search: </label>
      <input id="product_search" type="text" data-provide="typeahead">
    </div>

    <script src="js/jquery-1.10.2.js"></script>
    <script src="js/bootstrap-typeahead.js"></script>

    <script>
      $(document).ready(function($) {
        // Workaround for bug in mouse item selection
        $.fn.typeahead.Constructor.prototype.blur = function() {
          var that = this;
          setTimeout(function () { that.hide() }, 250);
        };

        $('#product_search').typeahead({
          source: function(query, process) {
            return ["Deluxe Bicycle", "Super Deluxe Trampoline", "Super Duper Scooter"];
          }
        });
      });
    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:4)

问题出在您的&#34;来源&#34; source应该是JSON对象的列表。你能改变吗?

    $('#product_search').typeahead({
      source: function(query, process) {
        return ["Deluxe Bicycle", "Super Deluxe Trampoline", "Super Duper Scooter"];
      }
    });

致:

$('#product_search').typeahead({
    source: [
        {id: 1, name: 'Deluxe Bicycle'},
        {id: 2, name: 'Super Deluxe Trampoline'},
        {id: 3, name: 'Super Duper Scooter'}
    ]
});

并告诉我它是否有效:)