Typeahead调用函数.val.open.colose等会引发错误

Typeahead calling functions .val .open .close etc throws errors

本文关键字:错误 colose 调用 函数 val open Typeahead      更新时间:2023-09-26

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

typeahead在输入元素上正常工作,因为它显示条目,但当我试图调用任何typeahead函数时,我会得到这个错误:

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

我的init代码如下:

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
})