2013-06-18 35 views
1

我想使用typeahead的匹配器函數來檢查我的搜索是否返回沒有結果。如果它沒有返回任何結果,我想在搜索欄的末尾附加一個div。然而,匹配器功能正在導致我的熒光筆中斷並返回隨機結果。有誰知道是否有一種方法可以在不使用匹配器功能的情況下完成此任務,或者在此情況下如何正確使用它?我想我可能會採取錯誤的做法。匹配器函數typeahead

$('.shop_search').typeahead({ 
     source: function (query, process) { 
      map = {}; 
      $.each(data, function (i, data) { 
       map[data.text] = { 
        address: data.text2, 
        name: data.text, 
        post: data.post 
       }; 
       shops.push(data.text); 
      }); 
      process(shops); 
      shops = []; 
     }, 

     minLength: 3, 
     matcher: function (item) { 
      if (item.indexOf(this.query) == -1) { 
       $(".dropdown-menu").append($('<li><button class="btn" >Advanced             Search</button></li>')); 
       return true; 
      } 
     }, 

     highlighter: function (item) { 
      var p = map[item]; 
      var itm = '' 
        + "<div class='typeahead_primary'>" + p.name + "</div>" 
        + "<div class='typeahead_secondary'>" + p.address + </div>" 
        + "</div>" 
        + "</div>"; 
      return itm; 
     }, 
    }); 

回答

0

在我看來,你忘了「

+ "<div class='typeahead_secondary'>" + p.address + </div>" 

應該

+ "<div class='typeahead_secondary'>" + p.address + "</div>"