Bootstrap Typeahead.js

房东的猫 提交于 2020-01-24 12:02:52

问题


I have been trying to get the bootstrap typeahead to work, with no luck whatsoever. I have pasted the code I have. I have reference Jquery-1.9.1 and typeahead.js. What am I doing wrong? Your help is much appreciated! Thanks.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<script src="Scripts/typeahead.js-master/test/vendor/jquery-1.9.1.js"></script>
<script src="Scripts/typeahead.js-master/src/typeahead.js"></script>
<title></title>
</head>
<body>
    <form>
    <div>
        <input type="text" data-provide="typeahead" autocomplete="off" data-source='["arizona","alaska"]' />
    </div>
    </form>
</body>
</html>

回答1:


It looks like you're getting confused between typeahead.js (by Twitter) and Twitter Bootstrap's Typeahead feature

You're including the library for typeahead.js

<script src="Scripts/typeahead.js-master/src/typeahead.js"></script>

but trying to use it like Twitter Bootstrap's typeahead feature.

Make sure you're including the Twitter Bootstrap library, if you want to use your current code:

<script type="text/javascript" src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>

Here's an example: jsFiddle




回答2:


I hope this is what you expection. Here is the complete code try with necessary dependencies.

<!DOCTYPE html>
<html>
    <head>
        <title>Member</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script type="text/javascript" src="js/jquery.js"></script>
        <script type="text/javascript" src="js/bootstrap.min.js"></script>
        <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">

    </head>
    <body>
<div class="well">  
<input type="text" class="span3" id="search" data-provide="typeahead" data-items="4" />  
</div>
<script>  
 var subjects = ['PHP', 'MySQL', 'SQL', 'PostgreSQL', 'HTML', 'CSS', 'HTML5', 'CSS3', 'JSON'];   
$('#search').typeahead({source: subjects})  
</script>
    </body>

</html>



回答3:


The Type head File with add two features:

  1. Type Space Bar Show All Auto Complete Data's
  2. If you Give Data with Space also Can be Sort and Display Data's

Change The Lookup Function This:

 lookup: function (event) {         
    var that = this,
        items;        
    if (that.ajax) {
        that.ajaxer();
    }    
    else  if (($.trim(that.$element.val())) == "") {
        that.query = that.$element.val();
        if (!that.query) {
            return that.shown ? that.hide() : that;
        }        
        items = that.source;        
        if (!items || !items.length) {
            return that.shown ? that.hide() : that;
        }
        return that.render(items.slice(0, that.options.items)).show();
    }
    else {
        that.query = $.trim(that.$element.val());
        if (!that.query) {
            return that.shown ? that.hide() : that;
        }        
        items = that.grepper(that.source);
        if (!items || !items.length) {
            return that.shown ? that.hide() : that;
        }
        return that.render(items.slice(0, that.options.items)).show();
    }
},


来源:https://stackoverflow.com/questions/17155439/bootstrap-typeahead-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!