我使用 typeahead/bloodhound 自动填充输入。我的网站有超过 20 个输入字段,所有输入字段都带有类 typeahead 和一个 id 属性。
我的问题是,我需要一个带有 %QUERY 和 %CID 的请求。%QUERY 是用户在输入字段之一中键入的搜索,%CID 应该是来自活动输入字段的 ID。
这是我的代码:
var search_artikel = new Bloodhound({
datumTokenizer: function(d) {
return d.tokens;
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: "modules/pc_config/ajax.php?load=search_articles&cat_id=%CID&search=%QUERY",
replace: function(url, query) {
//var cat = this.id;
//return url + "&cat_id=" + cat + "&search=" + query;
return url.replace('%QUERY', query).replace('%CID', $(this).attr('id'));
}
}
//remote: 'modules/pc_config/ajax.php?load=search_articles&search=%QUERY'
});
search_artikel.initialize(this.id);
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 4
},
{
name: 'phonenumber',
displayKey: 'number',
source: search_artikel.ttAdapter(),
templates: {
suggestion: Handlebars.compile([
'<p class="bold">{{number}}</p>',
'<p class="small"><i>{{name}} {{surname}}</i></p>',
'<p class="small"><i>{{address}}</i></p>',
].join(''))
}
});
除了 %CID 总是“未定义”外,一切正常。如何用输入字段中的活动/焦点 ID 替换 %CID?