2013-05-19 95 views
0
 $(function(){ 

     var orthoObjs = {}; 
     var orthoNames = []; 


     var throttledRequest = _.debounce(function(query, process){ 

      $.ajax({ 
       url: 'json/ortho4.json' 
       ,cache: false 
       ,success: function(data){ 


       orthoObjs = {}; 
       orthoNames = []; 


       _.each(data, function(item, ix, list){ 



       orthoNames.push(item.searchPhr); 


         orthoObjs[ item.searchPhr ] = item; 

        }); 


        process(orthoNames); 
       } 
      }); 
     }, 300); 


     $(".typeahead").typeahead({ 
      source: function (query, process) { 


       throttledRequest(query, process); 

      } 

     ,updater: function (item) { 

     var url = "orthoObjs[item.searchUrl]"; 

     window.location = url; 

什麼是獲得重定向工作的最佳方式?我看到過類似的問題,但無法讓這個工作。關於打字的文件不是很好。我爲每個函數使用underscore.js。只需要一個簡單的搜索查詢,當用戶選擇時重定向。bootstrap typeahead url/redirect

回答

1

我實際上得到了這個工作。我得到了一點幫助......但在這裏。還有就是JSON文件..

[ 
    { "id":1, "searchUrl":"invisalign.html", "name":"invisalign" } 
    ,{ "id":2, "searchUrl":"invisalign.html", "name":"invisalign teen" } 
    ,{ "id":3, "searchUrl":"clearbraces.html", "name":"clear braces" } 
] 

和HTML代碼....

很多好這裏的東西.. http://fusiongrokker.com/post/heavily-customizing-a-bootstrap-typeahead

而且搜索代碼..

<form method="post" id="myForm" class="navbar-search pull-left"> 
     <input 
      type="text" 
      class="search-query typeahead" 
      placeholder="Search Our Website" 
      autocomplete="off" 
      data-provide="typeahead" 
      /> 
      <i class="fa-icon-search icon-black"></i> 
</form> </li> 

$(function(){ 

     var bondObjs = {}; 
     var bondNames = []; 

     $(".typeahead").typeahead({ 
     source: function (query, process) { 

      //get the data to populate the typeahead (plus an id value) 
      $.ajax({ 
      url: '/json/bonds.json' 
      ,cache: false 
      ,success: function(data){ 

       bondObjs = {}; 
       bondNames = []; 


       _.each(data, function(item, ix, list){ 


       bondNames.push(item.name); 


       bondObjs[ item.name ] = item.searchUrl; 
       }); 


       process(bondNames); 
      } 
      }); 
     } 
     , updater: function (selectedName) { 

      window.location.href =bondObjs[ selectedName ]; 

     } 
     }); 
    }); 
    </script>