【问题标题】:Typeahead is not working with Jquery keyUp FunctionTypeahead 不适用于 Jquery keyUp 函数
【发布时间】:2015-02-11 12:50:10
【问题描述】:

我是 typeahead.js 的新手,并且正在使用引导搜索框进行自动完成......当我想在 document.ready 上加载数据(option1,option2..etc)时,我的 jquery 工作正常......这是以下代码...

$(function() {
            function displayResult(item) {
                $('.alert').show().html('You selected <strong>' + item.value + '</strong>: <strong>' + item.text + '</strong>');
            }

            $(document).ready(function() {
                    $.ajax({
                               type: 'GET',
                               url:'QuickSearch?City='+"Bengaluru",
                               dataType: 'json',
                               success: function(data){
                                   var j=jQuery.parseJSON(data.jsonAreaList);
                                   var cool=j;
                                 $('#demo1').typeahead({
                                     source: j,
                                    onSelect: displayResult
                                });

                                       },
                                       error: function(data) 
                                       {//When fails to load the Areas.

                                       }
                            });
                            return false;
                  }); 

所有区域都已正确加载....现在我的要求是我不想在 document.onready 上加载区域....喜欢从用户那里获取查询并将结果设置为预先输入的文本框.....这里这就是我正在尝试的

$(function() {
            function displayResult(item) {
                $('.alert').show().html('You selected <strong>' + item.value + '</strong>: <strong>' + item.text + '</strong>');
            }

            $(document).ready(function() {
             $("#SearchBox").keyup(function() {
                 var city=$("#SearchBox").val();
                    $.ajax({
                               type: 'GET',
                               url:'QuickSearch?City='+city,
                               dataType: 'json',
                               success: function(data){
                                   var j=jQuery.parseJSON(data.jsonAreaList);
                                   var cool=j;
                                 $('#demo1').typeahead({
                                     source: j,
                                    onSelect: displayResult
                                });

                                       },
                                       error: function(data) 
                                       {//When fails to load the Areas.

                                       }
                            });
                            return false;
                  });
    });
});

我不知道我做错了什么......:(

【问题讨论】:

    标签: jquery twitter-bootstrap typeahead.js bootstrap-typeahead


    【解决方案1】:

    你在ajax调用中的url查询字符串错误。

    试试:

    url:'QuickSearch?City='+city; //you have a typo in your code
    

    【讨论】:

    • 嘿@noob 在第二个例子中我得到了结果列表(Json)......在第二个例子中城市是动态的......:) 还有一件事绑定函数没有效果...... :(
    • 我知道,但你能看到 'city' 与 'City' 的拼写错误吗??
    • 您的代码现在可以工作了吗?这只是阻止它工作的错字吗?
    • No noob...警告我可以看到 json 格式的列表...但是在分配源时出了点问题...:)
    猜你喜欢
    • 2013-07-05
    • 1970-01-01
    • 2023-04-05
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    相关资源
    最近更新 更多