2

我是新手typeahead.js和工作自動完成與引導搜索框....我的jquery工作正常,當我想加載數據(option1,option2..etc)on的document.ready .....這裏是下面的代碼...Typeahead不工作與jquery keyUp函數

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

我不getti ng在哪裏我做錯了...... :(

回答

0

您的網址在Ajax調用查詢字符串是錯誤的。

嘗試:

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

嘿@noob在第二個例子中我得到的結果列表(JSON)...市是第二個例子動態.... :)還有一個件事綁定功能沒有效果... :( – 2015-02-11 13:14:09

+0

我知道,但你能看到拼寫錯誤'城市'的'城市'?? – 2015-02-11 13:17:03

+0

哦對不起noob這是一個typop錯誤... :) – 2015-02-12 03:57:58