【问题标题】:bootstrap 3 typeahead.js : Where could i find a working example code?bootstrap 3 typeahead.js:我在哪里可以找到有效的示例代码?
【发布时间】:2014-02-27 22:18:25
【问题描述】:

我正在寻找预先输入的工作示例。我想使用它,但我没有找到适用于 bootstrap 3 的 sn-p 代码。我想在这里进行自动完成 http://twitter.github.io/typeahead.js/examples/ 。如果你有一些链接,谢谢。

【问题讨论】:

  • 你能说得更具体点吗?任何功能或东西来澄清您的要求

标签: twitter-bootstrap-3 typeahead.js


【解决方案1】:

使用 Bootsrap 很难,因为 TYPEHEAD for exp.:

完整文档

    <!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <style>
            html {
              font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
              font-size: 18px;
              line-height: 1.2;
              color: #333;
            }
            .tt-dropdown-menu{
              text-align: left;
            }
            .typeahead,
            .tt-query,
            .tt-hint {
              padding: 8px 12px;
              font-size: 24px;
              outline: none;
            }
            .typeahead {
              background-color: #fff;
            }
            .tt-query {
              -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
                 -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
                      box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
            }
            .tt-hint {
              color: #999
            }
            .tt-dropdown-menu {
              width: 422px;
              margin-top: 12px;
              padding: 8px 0;
              background-color: #fff;
              border: 1px solid #ccc;
              border: 1px solid rgba(0, 0, 0, 0.2);
              -webkit-border-radius: 8px;
                 -moz-border-radius: 8px;
                      border-radius: 8px;
              -webkit-box-shadow: 0 5px 10px rgba(0,0,0,.2);
                 -moz-box-shadow: 0 5px 10px rgba(0,0,0,.2);
                      box-shadow: 0 5px 10px rgba(0,0,0,.2);
            }
            .tt-suggestion {
              padding: 3px 20px;
              font-size: 18px;
              line-height: 24px;
            }
            .tt-suggestion.tt-cursor {
              color: #fff;
              background-color: #0097cf;
            }
            .tt-suggestion p {
              margin: 0;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="countries"> <!-- This is a .countries in that examples.js -->
                <div class="demo">
                    <input class="typeahead form-control" type="text" placeholder="Countries" autocomplete="off" spellcheck="false" dir="auto" > <!-- This is a typehead class in that js-->
                    <input class="typeahead form-control" type="text" disabled="" autocomplete="off" spellcheck="false" style="visibility: hidden; ">
                    <pre aria-hidden="true" style="position: absolute; visibility: hidden; white-space: nowrap; font-size: 24px; font-style: normal; font-variant: normal; font-weight: 400; word-spacing: 0px; letter-spacing: 0px; text-indent: 0px; text-rendering: auto; text-transform: none;">
                    </pre>
                    <span class="tt-dropdown-menu" style="position: absolute; top: 100%; left: 0px; z-index: 100; display: none;">
                        <div class="tt-dataset-countries"></div>
                    </span>
                </div>
            </div>
            <script src="./typeahead.bundle.js"></script>
            <script src="./examples.js"></script>
        </div>
    </body>
</html>

在examples.js中是:

    $(document).ready(function() {
  var numbers;
var countries = new Bloodhound({
  datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.name); },
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  limit: 10,
  prefetch: {
    url: './countries.json',
    filter: function(list) {
      return $.map(list, function(country) { return { name: country }; });
    }
  }
});

countries.initialize();

$('.countries .typeahead').typeahead(null, {
  name: 'countries',
  displayKey: 'name',
  source: countries.ttAdapter()
});

});

本地方式

    $(document).ready(function() {
    var numbers;
    var countries = new Bloodhound({
        datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.name); },
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        limit: 10,
        local: [
            { name: 'Andorra' },
            { name: 'United Arab Emirates' },
            { name: 'Afghanistan'},
            { name: 'Antigua and Barbuda'},
            { name: 'Anguilla'},
        ]
    });

countries.initialize();

$('.countries .typeahead').typeahead(null, {
  displayKey: 'name',
  source: countries.ttAdapter()
});

});

【讨论】:

  • VeeeneX 感谢您的回复。我已经尝试过了,但自动完成不起作用请参阅pastebin.com/S6NRf3hJ
  • 是的,修复它的最佳方法是查看控制台的 exp。在 Chrome F12 中有:未捕获的错误:Bootstrap 的 JavaScript 需要 jQuery bootstrap.min.js:6 XMLHttpRequest 无法加载 twitter.github.io/typeahead.js/data/countries.json。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin 'localhost'。
  • VeeeneX 再次感谢您的帮助。我正在尝试完整的文档,但它不起作用。我在 chrome 调试控制台上收到一个 stange 错误。注意:显示了临时标题。我在问题上添加了屏幕截图
  • 朋友,问题出在你身边,尝试禁用 Chrome 中的扩展程序,或者(更好的方法)我已经写信给 asnwer
  • 感谢 VeeeneX,我终于明白发生了什么。如果我错了,请纠正我。当我们使用 typeahead 预取时。在页面加载时发送 XMLHttpRequest 以获取数据并将它们存储在本地存储中。数据将用于自动完成。但正如我们所知,XMLHttpRequest 遵循同源策略link。要发送 XMLHttpRequest,网页必须与响应请求的服务器应用程序位于同一服务器上。所以我已将预取 url 更改为本地路径。它的工作就像一个魅力。
猜你喜欢
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多