我希望预先输入建议菜单根据所选菜单显示不同的结果,如下所示:

var colors = ["red", "blue", "green", "yellow", "brown", "black"];
var numbers = ["One", "Two", "Three", "Four", "Five", "Six"];
var names = ["Ahmad", "Ali", "Hatem", "Wesam", "Nour", "Jeorge"];

$( "#search1" ).attr("autocomplete", "off");
$( "#search-fld" ).typeahead({source: colors});

$("#search-drop li a").click(function(){
    $("#search-btn").text($(this).text());
    if($(this).text() == 'People') {
        $( "#search-fld" ).typeahead({source: names});
    } else if($(this).text() == 'Salaries') {
        $( "#search-fld" ).typeahead({source: numbers});
    } else if($(this).text() == 'Jobs') {
        $( "#search-fld" ).typeahead({source: colors});
    }
});


这是jsfiddle的示例。
但是问题是,即使我选择了不同的子菜单,也仍然会出现相同的结果。

我想知道如何从不同的资源加载不同的预输入结果。

请指教,

==============================



==============================

最佳答案

这应该工作:

$("#search-drop li a").click(function(){
        $("#search-btn").text($(this).text());
        if($(this).text() == 'People') {
            $( "#search-fld" ).data('typeahead').source = names;
        } else if($(this).text() == 'Salaries') {
            $( "#search-fld" ).data('typeahead').source = numbers;
        } else if($(this).text() == 'Jobs') {
            $( "#search-fld" ).data('typeahead').source = colors;
        }
});

09-11 01:06