1
我正在尝试使用typeahead.js
在窗体中显示ajax结果。 起初我尝试Bloodhound
建议引擎默认与typeahead
来。但它没有显示服务器返回的所有项目(我只显示1或有时两个)。为什么typeahead.js没有显示ajax响应中的所有项目?
以下是我使用的代码;
$('.autocomplete').livequery(function() {
var input = this;
$(input).removeClass('autocomplete');
var _source = new Bloodhound({
limit: 25,
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Value'),
queryTokenizer: Bloodhound.tokenizers.obj.whitespace('Text'),
prefetch: $(input).attr('data-source'),
remote: {
url: $(input).attr('data-source') + '?query=%QUERY',
wildcard: '%QUERY'
}
});
_source.initialize();
$(input).typeahead({
hint: true,
highlight: true,
minLength: 0
}, {
displayKey: 'Text',
source: _source,
templates: {
notFound: 'No results found'
}
});
$(input).on('typeahead:selected', function (evt, item) {
$(input).parent().parent().find('input[type="hidden"]').val(item.Value);
});
})
然后我试着用没有血猎犬的方式做下面的代码,结果没有任何变化;
$('.autocomplete').livequery(function() {
var input = this;
$(input).removeClass('autocomplete');
$(input).typeahead({
hint: true,
highlight: true,
limit: 50,
minLength: 0
}, {
displayKey: 'Text',
source: function (query, syncResults, asyncResults) {
$.get($(input).attr('data-source') + '?query=' + query, function (data) {
asyncResults(data);
});
},
templates: {
notFound: 'No results found'
}
});
$(input).on('typeahead:selected', function (evt, item) {
$(input).parent().parent().find('input[type="hidden"]').val(item.Value);
});
})
服务器对请求的响应如下;
[
{
"Text": "Marketing Executive",
"Value": 1
},
{
"Text": "CEO",
"Value": 5
},
{
"Text": "Manager",
"Value": 6
},
{
"Text": "Accountant",
"Value": 7
}]
这是什么问题?以及如何获得打印以显示服务器返回的所有结果?
默认只有5建议将显示,使用'限制'选项来增加它。检查[文档](https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#options) –
@JSantosh'极限:50',检查问题。 – zeroflagL
你看不到50个结果吧? –