2015-06-29 28 views
0

我有我的代碼有問題,所以我有一個數組,我傳遞給模板,該陣列是這樣的:顯示值

Array 
(
[0] => Array 
    (
     [ref_article] => 1903 
     [ref_fournisseur] => sdsds 
     [lien_fournisseur] => www.four.com 
     [prix_ht] => 14.00 
     [gifts_number] => 3 
    ) 

[1] => Array 
    (
     [ref_article] => 1907 
     [ref_fournisseur] => sdsds 
     [lien_fournisseur] => www.four.com 
     [prix_ht] => 12.00 
     [gifts_number] => 1 
    ) 

) 

現在模板我這樣做:

for (var item in result) { 
    document.getElementById('order_information').setAttribute('class','display-on'); 
    document.getElementById('order_information').setAttribute('class','table'); 

    var html = '<td>' + result[item]['ref_article'] + '</td>' + 
       '<td>' + result[item]['ref_fournisseur'] + '</td>' + 
       '<td>' + 'description' + '</td>'+ 
       '<td>' + result[item]['lien_fournisseur'] + '</td>' + 
       '<td>' + result[item]['prix_ht'] + '</td>'+ 
       '<td>' + 'disponibilite' + 
       '<td>' + result[item]['gifts_number'] + '</td>'; 
    $("#content-order").html(html); 
    console.log(result[item]['ref_article']); 
} 

的問題是,只有最後<td></td>顯示在頁面上,在這種情況下,只有文章與[ref_article] = 1907。我究竟做錯了什麼?你能幫我嗎?在此先感謝

回答

5

問題是因爲您使用html()來添加內容 - 這將覆蓋#content-order元素中的任何預先存在的內容。相反,嘗試使用append()

$("#content-order").append(html); 

還要注意的是,你可以修改你的前兩行使用jQuery選擇和addClass()方法:

$('#order_information').addClass('display-on table'); 

如果要清除從以前添加的信息請求您可以在for循環之前使用.empty()附加新內容。這裏有一個完整的例子:使用此代碼

// $.ajax({... 

success: function(result) { 
    $("#content-order").empty(); // remove existing content 
    $('#order_information').addClass('display-on table'); 

    for (var item in result) { 
     var html = '<td>' + result[item]['ref_article'] + '</td>' + 
       '<td>' + result[item]['ref_fournisseur'] + '</td>' + 
       '<td>description</td>' + // removed redundant string concatenation 
       '<td>' + result[item]['lien_fournisseur'] + '</td>' + 
       '<td>' + result[item]['prix_ht'] + '</td>'+ 
       '<td>disponibilite</td>' + // added missing </td> 
       '<td>' + result[item]['gifts_number'] + '</td>'; 
     $("#content-order").append(html); 
     console.log(result[item]['ref_article']); 
    } 
} 
+0

我更新我的回答對你 –

0

嘗試

var html = '<table>'; 
for (var item in result) { 
    html += '<td>' + result[item]['ref_article'] + '</td>' + 
     '<td>' + result[item]['ref_fournisseur'] + '</td>' + 
     '<td>description</td>' + // removed redundant string concatenation 
    '<td>' + result[item]['lien_fournisseur'] + '</td>' + 
     '<td>' + result[item]['prix_ht'] + '</td>' + 
     '<td>disponibilite</td>' + // added missing </td> 
    '<td>' + result[item]['gifts_number'] + '</td>'; 

    console.log(result[item]['ref_article']); 
} 
html += '</table>'; 
$("#content-order").html(html);