gpt4 book ai didi

javascript - 清除 Typeahead.JS 查询时缺少源错误

转载 作者:行者123 更新时间:2023-11-29 15:37:35 25 4
gpt4 key购买 nike

我正在尝试在字段 ("#addtags") 上使用 Twitter 的 typeahead.js 来建议要添加的标签,但是当我选择某些内容时,该字段会保留我刚刚输入的值应该清除。

我已经尝试了此处提供的解决方案 ( Set selected value of typeahead ),即使用 $("#id").typeahead('setQuery', query); 清除字段。但是,当我这样做时,我的控制台出现错误:Uncaught Error: missing source.

有解决办法吗?清除该字段的代码是底部上方的一行,其余内容提供给上下文。

//===========================
//Typeahead.js
//===========================

var tagApi = $("#addtags").tagsManager();
var addtags = new Bloodhound({
datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d['tag']); },
queryTokenizer: Bloodhound.tokenizers.whitespace,
dupChecker:true,
// remote: '/tags/tags/search.json?q=%QUERY',
prefetch: {url: '/tags/tags/search.json?q='}
});

addtags.initialize();

$('#addtags').typeahead({
hint:true,
highlight:true,
autoselect:false,
dupChecker:true,
},
{
displayKey: 'tag',
source: addtags.ttAdapter(),
}).on('typeahead:selected', onTagSelect);

function onTagSelect($e, datum) {
tagApi.tagsManager("pushTag", datum['tag']);

//This is where I'm trying to clear the input:
$('#addtags').typeahead('setQuery', '');

};

最佳答案

我创建了一个 jsfiddle 来展示如何清除选定的建议:

http://jsfiddle.net/Fresh/ecq0c0ph/

尝试搜索一部电影,然后选择一个建议;该建议将用于填充某些字段,然后查询输入控件将被清除。

简而言之,要在选择时清除建议,为 Typeahead 的选定 event 创建一个事件处理程序,然后使用其 API 设置 value为空字符串,例如

.bind("typeahead:selected", function () {
$('.typeahead').typeahead('val', '');
});

关于javascript - 清除 Typeahead.JS 查询时缺少源错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25615531/

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