gpt4 book ai didi

javascript - Typeahead 调用函数 .val .open .close 等会抛出错误

转载 作者:搜寻专家 更新时间:2023-11-01 04:36:43 25 4
gpt4 key购买 nike

我很难调用 typeahead.js 函数,例如 valopenclose 等。

typeahead 在输入元素上正常工作,因为它显示条目,但是当我尝试调用任何 typeahead 函数时,我收到此错误:

Uncaught Error: one of local, prefetch, or remote is requiredmessage: "one of local, prefetch, or remote is required

我的初始化代码如下:

var currentMedicalAid;

var medicalAidList = $('#medicalAidList');


var medicalAidengine = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('MedicalAid1'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 5,
prefetch: {
url: '/api/Search/GetMedicalAidList',
}
});


medicalAidengine.initialize();


medicalAidList.typeahead(null, {
name: 'MedicalAid1',
displayKey: 'MedicalAid1',
// `ttAdapter` wraps the suggestion engine in an adapter that
// is compatible with the typeahead jQuery plugin
source: medicalAidengine.ttAdapter()
}).blur(function () {
match = false;
for (var i = medicalAidengine.index.datums.length - 1; i >= 0; i--) {
if ($(this).val() == medicalAidengine.index.datums[i].MedicalAid1) {
currentMedicalAid = medicalAidengine.index.datums[i].MedicalAidID;
match = true;
}
};
if (!match) {
currentMedicalAid = '00000000-0000-0000-0000-000000000000';
$(this).val('');
medicalAidList.typeahead('val', ''); // throws error
}
});;

每次我运行这些函数中的任何一个时,我都会得到一个错误:

medicalAidList.typeahead('val', '');
or
$('#medicalAidList').typeahead('val','');

任何帮助将不胜感激

谢谢

最佳答案

initialize 方法是异步的。当 Bloodhound 引擎未完全初始化时,您的某些代码可能正在运行。您可能需要将其余代码包装在回调中:

// initialize returns a jQuery promise, so we can call its done method
medicalAidengine.initialize().done(function(){

// Rest of your code

})

关于javascript - Typeahead 调用函数 .val .open .close 等会抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26561043/

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