2016-08-03 103 views
0

我有Bootstrap Typeahead的这个问题。BS Typeahead不显示结果

HTML标记:

<div class="form-group"> 
    <label for="">Recipients</label> 
    <input id="recipients" name="recipients" 
     autocomplete="off" 
     class="form-control" 
     data-role="tagsinput"> 
</div> 

的JavaScript:

$('#recipients').tagsinput({ 
    allowDuplicates: false, 
    trimValue: true, 
    confirmKeys: [13, 44], 
    typeahead: { 
     source: function(queryForHints) { 
      if (queryForHints.length < 4) 
       return ''; 
      var parameters = { 
       'queryForHints': queryForHints 
      }; 
      jQuery.ajax({ 
       url: "/xxxx/xxxx", 
       data: parameters, 
       type: "POST", 
       dataType: "json", 
       success: function(data) { 
        return (data); 
       }, 
       error: function(data) { 
        console.log("error for xxxxx/xxxxx"); 
       }, 
       async: true 
      }); 
     } 
    } 
}); 

阿贾克斯电话后,我得到这个数组:

[{ 
    "value": "+393334029137", 
    "text": "Dean Leandra (+393334029137)" 
}, { 
    "value": "+393333419836", 
    "text": "Alfonso Erasmus (+393333419836)" 
}, { 
    "value": "+393173833341", 
    "text": "Travis Aquila (+393173833341)" 
}, { 
    "value": "+393334998841", 
    "text": "Conan Preston (+393334998841)" 
}] 

的问题是:我什么也看不到:(没有出现typeahead不起作用

在控制台中,我得到这个错误

bootstrap-tagsinput.js:331 Uncaught TypeError: Cannot read property 'success' of undefined

你能帮我解决这个问题?

+1

什么是您的AJAX调用数组中的null?错字? – James

+0

现在我决心在你提出的问题......现在从AJAX调用返回我有: [{ “值”:“393334029137”, “文”:“院长Leandra(393334029137)” }, { “值”: “393333419836”, “文本”: “阿方索的Erasmus(393333419836)” },{ “值”: “393173833341”, “文本”:“特拉维斯雕(393173833341) “ },{ ”value“:”+393334998841“, ”text“:”Conan Preston(+393334998841)“ }] –

+0

帮助我们为您提供帮助。你需要清楚3件事情。 1.您使用的是哪个版本的引导程序。 2.你使用的是哪个插件3.在ajax调用 – naveen

回答

0
success: function(data) { 
    var sourceData = []; 
    for (var i = 0; i < data.length; i++) { 
     sourceData.push(data[i].text); 
    } 
return (sourceData); 

您从JSON响应有什么是对象数组。你想看看text作为我所推荐的建议。所以你需要从你的对象得到text,因为typeahead源需要一个字符串数组。将text值推入另一个字符串数组并返回。我还没有测试过代码,但它应该适合你。

您遇到的另一个问题是您正在进行异步的AJAX调用。这就是为什么你没有从成功中取得成功。