1

如果我将静态数组作为源,Typeahead 可以工作,但是当我尝试使用函数生成数组时,它不会填充其下拉列表。

$("#mix-artist" ).typeahead({
        source: function(query, process) {
            Mix.searchArtist(query);
        },          
        minLength: 3,
    });

源函数:

searchArtist: function(query) {
    $.get(API_MAIN_URL, {a: "search.artists", q: query}, 
        function(data) {
            artists = [];
            query = query.toLowerCase();
            if (data['aData']) {
                for (var i = 0; i <= data['aData'].length; i++) {
                    if (data['aData'][i] && data['aData'][i]['sName'].toLowerCase().indexOf(query) == 0) {
                        artists.push(data['aData'][i]['sName']);
                    }
                }
            }
            console.log(artists);
            return artists;
        });
}

源函数的示例输出:

["Arcangel", "Arc Angels", "Arcade Fire", "Arctic Monkeys", "Archers of Loaf", "Architecture in Helsinki", "Archie Shepp", "Arcade", "Arch Enemy", "Arcadia", "Archie Eversole"]

我该怎么做才能提前输入接受这个数组?

4

1 回答 1

2

你需要你的searchArtist函数来接受process回调。而不是返回数组,您应该调用process(artists).

于 2012-12-11T22:35:38.357 回答