2017-05-26 202 views
0

我試圖整合Twitter的typeahead.js,爲了這個,我從https://github.com/twitter/typeahead.js下載庫,其中包括typeahead.bundle.min.jstypeahead.js不會觸發源的功能

我加入以下

<input type="text" class="form-control typeahead font-normal"> 
$('.typeahead').typeahead({ 
    source: function(query, process) { 
     console.log(query); 
    } 
}); 

問題它是否觸發源函數,我期待它在控制檯中打印查詢。它沒有。

這裏可能有什麼問題?

謝謝。

以下解決方案工作。

如答案指出,添加以下庫第一

  1. jquery.min.js
  2. typeahead.bundle.min.js(https://twitter.github.io/typeahead.js/
  3. typeahead.jquery。 min.js(https://twitter.github.io/typeahead.js/
  4. bootstrap3-typeahead.min.js(https://github.com/bassjobsen/Bootstrap-3-Typeahead

然後初始化下面的代碼

$('.typeahead').typeahead({ 
    source: function(query, process) { 
     process([ 
      {id: "1", name: "Display name 1"}, 
      {id: "2", name: "Display name 2"} 
     ]); 
    } 
}); 

和它的作品。

回答

1

試試這個, 您必須添加引導預輸入JS

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

的jsfiddle爲同一 https://jsfiddle.net/aa7zyyof/17/

+0

你是對的,這個現在被觸發,但是當我嘗試返回JSON對象,它不顯示,如何顯示結果? –

+0

我明白了,更新問題以供參考。謝謝。 –

+1

還有另一個typeahead.js的「維護」版本... https://github.com/corejavascript/typeahead.js – Artistan