我从ajax源代码中提取建议时,使用了Type预报/猎犬:
var protags = new Bloodhound({
datumTokenizer: function(protags) {
return Bloodhound.tokenizers.whitespace(protags.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/ajax/getinfo.php?q=%QUERY',
wildcard: '%QUERY',
filter: function(response) {
return response.protags;
}
}
});getinfo.php的JSON结果如下所示:
{
"protags": [
{"tag": {
"tagid": "1",
"tagtitle": "titleone"}
},
{"tag": {
"tagid": "2",
"tagtitle": "titletwo"}
},
{"tag": {
"tagid": "3",
"tagtitle": "titlethree"}
}]}我能够检索所需的所有信息(tagtitle和tagid),并使用以下方法显示:
$('.typeahead').typeahead(
{ hint: true,
highlight: true,
minLength: 1
},
{
name: 'protags',
displayKey: function(protags) {
return protags.tag.tagtitle+'-'+protags.tag.tagid;
},
source: protags.ttAdapter()
});但我对此感到困惑:我如何才能在建议字段中只显示标签标题,但是如何获得protags.tag.tagid来执行更多的服务器端操作?
发布于 2016-04-13 12:28:09
使用:select事件(v0.11.x)或:selected (v.0.10.x)。阅读猎犬/打字机文档,因为它们在0.10.x和0.11.x之间做了很多更改
我使用的是0.10.5,在我的例子中,如下所示:
编辑:看看你的json,我不知道什么数据进入模板和:选择的函数。您可能需要使用data.protags.tag等
$(selector).typeahead(
// options, key, source etc
templates: {
suggestion: function (data) {
return '<div class="tt-name">' + data.tag.tagtitle + '</div>';
}
}
// end of options
)
.on('typeahead:selected',
function(event, data) {
// you should be able to get your data here, if I'm correct like so:
console.log(data.tag.tagid);
}
);https://stackoverflow.com/questions/36596951
复制相似问题