简体   繁体   English

无法让Twitter Typeahead在Bootstrap中工作

[英]Can't get Twitter Typeahead to work in Bootstrap

This might be a noob issue, but I cannot figure out why this code is not working. 这可能是一个noob问题,但我无法弄清楚为什么这段代码不起作用。 FYI this is just a sample list of counties pulled from a state with just a few. 仅供参考,这只是一个从少数国家撤出的县的样本清单。 I pull the list from php based on earlier inputs, but that is not the issue, the list is fine, its autocomplete not working. 我根据之前的输入从php中提取列表,但这不是问题,列表很好,它的自动完成功能不起作用。

http://jsfiddle.net/s4wx9/ http://jsfiddle.net/s4wx9/

<div>Local County:
    <input type="text" class="input-block-level required typeahead" id="county" name='county' placeholder="Local County" />
    <div>

<script>
    $("#county").typeahead({
        local: ["Apache", "Cochise", "Coconino", "Gila", "Graham", "Greenlee", "La Paz", "Maricopa", "Mohave", "Navajo", "Pima", "Pinal", "Santa Cruz", "Yavapai", "Yuma"]
    });
</script>

Just replace local by source in your javascript 只需在您的JavaScript中替换local source

$("#county").typeahead({
        source: ["Apache", "Cochise", "Coconino", "Gila", "Graham", "Greenlee", "La Paz", "Maricopa", "Mohave", "Navajo", "Pima", "Pinal", "Santa Cruz", "Yavapai", "Yuma"]
    });

Local is used with the standalone Typeahead library. Local与独立的Typeahead库一起使用。

EDIT: Here's an updated jsfiddle: http://jsfiddle.net/s4wx9/1/ 编辑:这是一个更新的jsfiddle: http//jsfiddle.net/s4wx9/1/

There are two problems 有两个问题

  1. You are using a very old version of bootstrap(2.1.1) which is not compatible with jquery 1.9 version (There are many deprecated features removed in this version including the $.browser extension which is used by bootstrap) - if you cannot upgrade to a new version of bootstrap for the time being you can include the migration plugin to fix this problem 您正在使用与jquery 1.9版本不兼容的非常旧版本的bootstrap(2.1.1)(此版本中删除了许多已弃用的功能 ,包括引导程序使用的$ .browser扩展名) - 如果您无法升级到一个新版本的bootstrap目前你可以包含迁移插件来解决这个问题
  2. need to set the source option instead of local 需要设置源选项而不是本地选项

try 尝试

$("#county").typeahead({
    source: ["Apache", "Cochise", "Coconino", "Gila", "Graham", "Greenlee", "La Paz", "Maricopa", "Mohave", "Navajo", "Pima", "Pinal", "Santa Cruz", "Yavapai", "Yuma"]
});

Demo: Fiddle 演示: 小提琴

It works to me. 它对我有用。 Run this Code: 运行此代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.1/bootstrap3-typeahead.min.js"></script>

<div>Cnty:
    <input type="text" class="input-block-level required typeahead" id="county" name='county' placeholder="Local County" />
    <div>


<script>
$("#county").typeahead({
   source: ["Apache", "Cochise", "Coconino", "Gila", "Graham", "Greenlee", "La Paz", "Maricopa", "Mohave", "Navajo", "Pima", "Pinal", "Santa Cruz", "Yavapai", "Yuma"]
    });
</script>

Include:These above 包括:以上这些

jquery-2.1.1.min.js
bootstrap.min.css
bootstrap.min.js

You are done. 你完成了。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM