gpt4 book ai didi

javascript - 从 Typeahead 向 Bloodhound 传递参数?

转载 作者:数据小太阳 更新时间:2023-10-29 06:06:14 26 4
gpt4 key购买 nike

我正在使用 Typeahead 设置表单。我有两个彼此相邻的输入字段,我需要对每个输入字段进行自动完成。我的 HTML 看起来像这样:

<select id="numerator">
<option value="presentation">presentation</option>
<option value="chemical">chemical</option>
</select>
<input id="numeratorId" class="typeahead" type="text" />
<br/>
<select id="denominator">
<option value="presentation">presentation</option>
<option value="chemical">chemical</option>
</select>
<input id="denominatorId" class="typeahead" type="text" />

每个 input字段将通过查看 API 端点自动完成。这应该是 /api/1.0/code?type=presentation&code=123 的形式或 /api/1.0/code?type=chemical&code=123 .

type 的值API 调用中的参数应取决于 <select> 的值每个输入字段旁边的元素。

我遇到的问题是我不知道如何告诉 Bloodhound type 是什么参数应该是。

理想情况下,我想将其传递给 Bloodhound,但我不知道该怎么做。这是我的 JavaScript:

var bnfMatches = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/api/1.0/code?',
replace: function(url, uriEncodedQuery) {
url = url + 'code=' + uriEncodedQuery;
// How to change this to denominator for denominator queries?
val = $('#numerator').val();
if (!val) return url;
return url + '&code_type=' + encodeURIComponent(val)
}
}
});

$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 2
},
{
name: 'states',
displayKey: 'id',
source: bnfMatches.ttAdapter()
});

如果有任何建议,我将不胜感激。

最佳答案

尝试

html

注意,在 input 元素中删除了重复的 id numeratorId;分别替换 numeratorId , denominatorId 。这也允许在 replace 函数中选择 select 元素。

<select id="numerator">
<option value="presentation">presentation</option>
<option value="chemical">chemical</option>
</select>
<input id="numeratorId" class="typeahead" type="text" />
<br/>
<select id="denominator">
<option value="presentation">presentation</option>
<option value="chemical">chemical</option>
</select>
<input id="denominatorId" class="typeahead" type="text" />

js

请注意,bnfMatches 未初始化。在 Bloodhound 设置之后添加了 bnfMatches.initialize();

var bnfMatches = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/api/1.0/code?',
replace: function(url, uriEncodedQuery) {
var val = $(".typeahead").filter(":focus")
.attr("id").slice(0, -2);
var res = (url + "type=" + $("#" + val).val() + "&code="
+ encodeURIComponent(uriEncodedQuery));
console.log(res);
return res
}
}
});

bnfMatches.initialize();

$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 2
},
{
name: 'states',
displayKey: 'id',
source: bnfMatches.ttAdapter()
});

var bnfMatches = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/api/1.0/code?',
replace: function(url, uriEncodedQuery) {
var val = $(".typeahead").filter(":focus")
.attr("id").slice(0, -2);
var res = (url
+ "type=" + $("#" + val).val() + "&code="
+ encodeURIComponent(uriEncodedQuery));
console.log(res);
return res
}
}
});

bnfMatches.initialize();

$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 2
},
{
name: 'states',
displayKey: 'id',
source: bnfMatches.ttAdapter()
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script src="http://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>

<select id="numerator">
<option value="presentation">presentation</option>
<option value="chemical">chemical</option>
</select>
<input id="numeratorId" class="typeahead" type="text" />
<br/>
<select id="denominator">
<option value="presentation">presentation</option>
<option value="chemical">chemical</option>
</select>
<input id="denominatorId" class="typeahead" type="text" />

关于javascript - 从 Typeahead 向 Bloodhound 传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29631662/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com