- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Twitter 输入,但发现很难划分 mysql 查询的结果。
我的 json 输出如下所示:{"id":"1","name":"Bravo"}
在当前状态下,typeahead 中的结果显示名称和 id ,我希望能够仅显示名称,但输入的实际提交值是 id。我的脚本如下:
<script type="text/javascript">
// Instantiate the Bloodhound suggestion engine
var suggestions = new Bloodhound({
datumTokenizer: function (datum) {
return Bloodhound.tokenizers.whitespace(datum.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: 'includes/livesearch.php?key=%QUERY',
wildcard: '%QUERY',
filter: function (name) {
// Map the remote source JSON array to a JavaScript object array
return $.map(name, function (name) {
return {
value: name
};
});
}
}
});
// Initialize the Bloodhound suggestion engine
suggestions.initialize();
// Instantiate the Typeahead UI
$('.typeahead').typeahead({
hint: true,
minLength: 2
}, {
limit: 7,
displayKey: 'value',
source: suggestions.ttAdapter(),
});
</script>
非常欢迎任何帮助或建议我如何实现这一目标。
谢谢!
最佳答案
您可以创建一个对象来存储当前检索到的值。在filter
Bloodhound
的选项将对象属性设置为 name.name
,并将值设置为 name.id
.
仅返回并显示name
检索到的属性JSON
,使用index
$.map()
的参数检查对象的属性名称。如果属性是"name"
返回{value:name}
,否则返回null
.
使用typeahead:selected
设置 <input type="hidden">
值的事件<form>
内的元素使用 .typeahead
的当前值input
作为先前存储的初始返回值对象的属性引用 filter
。设置将值存储到空对象的变量引用。
<form>
<input class="typeahead" type="text" placeholder="search">
<input type="hidden" name="result" value="" />
<input type="submit">
</form>
$(function() {
// Instantiate the Bloodhound suggestion engine
var curr = {};
var suggestions = new Bloodhound({
datumTokenizer: function(datum) {
return Bloodhound.tokenizers.whitespace(datum.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: 'includes/livesearch.php?key=%QUERY',
wildcard: '%QUERY',
filter: function (name) {
curr[name.name] = name.id;
// Map the remote source JSON array to a JavaScript object array
return $.map(name, function (name, index) {
return index === "name" ? {
value: name
} : null;
});
}
}
});
// Initialize the Bloodhound suggestion engine
suggestions.initialize();
// Instantiate the Typeahead UI
$(".typeahead").typeahead({
hint: true,
minLength: 2
}, {
limit: 7,
displayKey: 'value',
source: suggestions.ttAdapter(),
})
.on("typeahead:selected", function (e, datum) {
$("form [name=result]").val(curr[datum.value]); // set value here
curr = {};
});
})
关于javascript - 预先输入 Bloodhound 排序返回值。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41333654/
我正在尝试加载人名的自动完成信息以进行提前输入,然后如果我已经有了结果,则不必再次查询服务器。 例如,如果我搜索一个人的姓名,并且从远程查询中检索该人(以及其他人)的数据,那么当我删除该姓名并搜索姓氏
我正在使用 Twitter 输入,但发现很难划分 mysql 查询的结果。 我的 json 输出如下所示:{"id":"1","name":"Bravo"} 在当前状态下,typeahead 中的结果
我有一个这样的脚本: $(document).ready(function() { var searchType = $('select[name=searc
这是我的代码: tagsProcessor(){ const suggestions = [{value: 'string1'}, {value: 'string2'}, {value
我使用 Bloodhound 作为提前输入的建议引擎。 问题是当我在 Bloodhound 中使用远程源时。它进行查询并获取结果,但是当我搜索结果时没有返回任何内容。 更具体地说,这段代码工作得很好:
我的印象是在执行远程调用时不需要使用 Bloodhound。我尝试运行此代码: $("#iban2").typeahead({ hint: false, minLength: 4,
我已经使用远程数据源实现了 typeahead.js 和 bloodhound,它基本上按预期工作。但是,我已将 typeahead 上的 minLength 设置为 2,虽然我可以看到 ajax 请
我似乎无法通过远程查询来正确使用 POST。 var creditors = new Bloodhound({ datumTokenizer: function (d) { r
我正在尝试让 twitter typeahead 正常工作,但 type advance 功能不起作用。它不显示自动完成部分。这是 JavaScript 代码。 var films = new Bl
我想使用“startswith”过滤器来过滤结果。现在,下面的代码获取与结果中任何单独单词匹配的所有内容。因此,当用户输入“ex”时,“example”和“一二示例”都会被过滤掉。如何更改此行为以便仅
我正在尝试实现类似于 stackoverflow 标签建议输入字段的功能。在数据库中,我有两个表:一个用于标签,另一个用于标签别名或标签同义词(tagSynonyms)。 当用户键入术语时,搜索引擎应
我正在使用 Bloodhound 从数据库中获取数据,然后使用 twitter 提前输入以显示搜索框下方的选项。 目前,bloodhound 部分正在查找所需的对象,但 typeahead 未显示它们
我正在使用 Twitter typeahead 和 Bloodhound 建议引擎,一切正常。下面是我的代码片段 // instantiate the bloodhound suggestion en
我尝试将下面的代码与 typeahead.js v 0.10 一起使用 // instantiate the bloodhound suggestion engine var numbers = ne
这是我的javascript: $(document).ready(function () { var people = new Bloodhound({
我正在使用 typeahead.js 0.11.1 并尝试对来自远程源的结果进行排序。根据代码,应该可以覆盖 bloodhound 的默认排序功能。但是我的排序函数从未被调用过。识别函数的计数相同。
我是第一次使用 twitter typeahead.js。我首先从一个简单的本地数组开始,然后让它开始工作。 作为下一步,我现在正尝试使其与 .json 文件一起使用。尽管我尝试了几种选择,但我无法使
我将 Typeahead(带有默认建议)与 Bloodhound 一起使用,到目前为止一切正常。但是,当我尝试动态更改建议的值时遇到了一些问题。 例如,当我选择其中一个元素时,我有一系列可用的建议,例
如何提前输入以提交数据。 这里是jsfiddle上的相关代码http://jsfiddle.net/6W3Qu/2/ . 代码副本: var numbers = new Bloodhound({ da
我正在使用 Typeahead/Bloodhoud 进行公司搜索,但自动完成部分没有显示足够的匹配项。 ------------ 搜索------------ 我应该至少看到 5 个结果,因为限制是
我是一名优秀的程序员,十分优秀!