2013-02-19 123 views
0

喜的朋友我試圖我的AJAX輸出追加到我的下拉列表....追加Ajax的jQuery的數據到下拉列表中的CakePHP

我AJAX功能: -

$.ajax({ 
    url: "getcolumn", 
    data: {value: value}, 
    type: "POST", 
    success: function(output) { 
     var column = output;//here i am assigning the output to another variable 
     var mySelect = $('#table_name'); 
     $.each(column, function(val, text) { 
      mySelect.append($('<option></option>').val(val).html(text)); 
     }); 

我的形式下拉: -

echo $this->Form->input('Column', array(
    'label' => 'Select the column name below', 
    'name' => 'tablename', 
    'id' => 'table_name', 
    'options' => array('null') 
)); 

我想從AJAX輸出追加到上述下拉框.... 我試圖在我的AJAX成功處理功能增加,但沒有工作。誰能幫我.... 和輸出是JSON形式.....

+0

你可以在這裏發佈'json'輸出嗎? – Jai 2013-02-19 11:08:14

+0

['id','venue','address',....] – 2013-02-19 11:16:57

+0

我需要查看json結構,並且請具體說明要在選項文本中顯示json的哪個部分。 – Jai 2013-02-19 11:21:02

回答

0

試試這個:

$.ajax({ 
url: "getcolumn", 
data: {value: value}, 
type: "POST", 
contentType: "application/json", //<----add this 
dataType: "json"     //<----and this 
success: function(output) { 
    var column = output;//here i am assigning the output to another variable 
    var mySelect = $('#table_name'); 
    $.each(column, function(val, text) { 
     mySelect.append($('<option></option>').val(val).html(text.id)); 
    }); 
} 
}); 
+0

選項不能正常工作.... – 2013-02-19 11:30:18

+0

你有一個有效的json結構。請嘗試在這裏:http://jsonlint.com/ – Jai 2013-02-19 11:31:48

+0

它是一個有效的JSON – 2013-02-19 11:35:39

0

我sugest你getcolumn腳本 創建正確的JSON您的jQuery每個工作json的應該是這樣的:

[{id: 86,label: "venue and address"}, {id: 87,label: "venue and address"}] 

,然後修改了一下你的循環:

mySelect.append($('<option></option>').val(text.id).text(text.label));