2013-06-18 36 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>"