javascript – Twitter Bootstrap Typeahead部队选择

前端之家收集整理的这篇文章主要介绍了javascript – Twitter Bootstrap Typeahead部队选择前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的应用程序中使用Bootstrap Typeahead插件,这是我的代码(这是一个例子).
我正在寻找一种方法来验证用户的选择(基本上如果输入与任何结果不匹配 – >清空模糊框).结果必须匹配.我到处搜索,找不到东西.非常感谢您的帮助.
$('#search').typeahead({
        source: function (query,process) {
            var states = [];
            var map = {};

            var data = [
                { "stateCode": "CA","stateName": "California" },{ "stateCode": "AZ","stateName": "Arizona" },{ "stateCode": "NY","stateName": "New York" },{ "stateCode": "NV","stateName": "Nevada" },{ "stateCode": "OH","stateName": "Ohio" }
            ];

            $.each(data,function (i,state) {
                map[state.stateName] = state;
                states.push(state.stateName);
            });

            process(states);
        }
    });

解决方法

如果提取出数据,状态和映射,则可以在.blur函数中使用它们进行查找:
var data = [
            { "stateCode": "CA","stateName": "Ohio" }
        ];
 var states = [];
 var map = {};

 $.each(data,state) {
    map[state.stateName] = state;
    states.push(state.stateName);
 });

 $('#search').typeahead({
    source: function (query,process) {
        process(states);
    }
}).blur(function(){
    if(states[$(this).val()] == null) {
      $('#search').val('');
    }
});
原文链接:https://www.f2er.com/js/158710.html

猜你在找的JavaScript相关文章