bootstrap typeahead with下拉按钮

时间:2013-04-03 21:01:44

标签: javascript jquery twitter-bootstrap bootstrap-typeahead

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

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的示例。 但问题是,即使我选择不同的子菜单,也会出现相同的结果。

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

请咨询,

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

Dynamic typeahead error

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

New Error

1 个答案:

答案 0 :(得分:2)

这应该有效:

$("#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;
        }
});