2012-11-29 47 views
0

我在我的文本框中鍵入時使用以下代碼獲取建議列表。Bootstrap 2.2 Typeahead問題

JS

$("#address").typeahead({ 
    source: function(query,typeahead){ 
     $.ajax({ 
      url: "http://localhost/disc/autocomplete/"+query, 
      type: "GET", 
      dataType: "JSON", 
      async: true, 
      success: function(data){ 
       typeahead.process(data); 
      } 
     }); 
    }, 
    property: 'address', 
    items:8, 
    onselect: function (obj) { 
     // window.location = obj.url; 
    } 
}); 

PHP

$count=0; 
    foreach ($query->result() as $row) 
    { 
     $count++; 
     $item['value'] = $row->address; 
     $item['id'] = $count; 
     $output[] = $item; 
    }   
    echo json_encode($output); 

TextBox

<input type="text" id="address" autocomplete="off" name="address" class="input-block-level" placeholder="Street address.."> 

現在,當我在文本框中鍵入我收到錯誤

Uncaught TypeError: Object function(){return a.apply(c,e.concat(k.call(arguments)))} has no method 'process' 

編輯:

$("#typeahead").typeahead({ 
    source: function(query,callback){ 
     $.ajax({ 
      url: "http://192.168.8.132/disc/autocomplete/"+query, 
      type: "POST", 
      dataType: "JSON", 
      async: false, 
      success: function(data){     
       //this.process(data); 
       callback(data); 
      } 
     }); 
    }, 
    items:8, 
    onselect: function (obj) { 
    // window.location = obj.url; 
    } 
}); 

回答

2

什麼是預輸入?在調用進程成員之前,您顯然需要使用它。 (instanciation,無論類型應該是什麼)。

EDIT 1:

source: function(query,callback/** you need that to execute something after the XMLHttp request has returned**/){ 
     $.ajax({ 
      url: "http://localhost/disc/autocomplete/"+query, 
      type: "GET", 
      dataType: "JSON", 
      async: true, 
      success: function(data){ 
       /** execute the callback here do whatever data processing you want before**/ 
       callback(data); 
      } 
     }); 
    }, 

在功能編程它被稱爲連續(如GOTO指令)。

編輯2:

你不決定什麼是回調,回調函數,所以不要嘗試做任何事情比你接收到的數據稱這是別人。再次,回調是一個GOTO像指令,它是一個延續,你不控制它。你需要用數據作爲參數來執行它。

+0

如果你知道我的意思,'typeahead'是'Bootstrap'的一個特性。我是否需要實例化tat?如果是,我該怎麼做? – Deepak

+0

我知道Twitter引導程序是什麼,以及前面提到的類型,我正在討論源回調以及您傳遞給源函數的typeahead對象。它是什麼 ? – mpm

+0

顯然我並沒有理解那部分。我沒有通過任何東西進入回調。你能告訴我在那裏做什麼?其實我的代碼看起來就像現在一樣。我沒有隱藏任何東西.. – Deepak