2012-05-09 52 views
1

嗨im從數據庫中使用JSON和ajax檢索一些數據。當我抓住我需要的信息時,我希望它顯示在div中的新行中,即時消息將它放入。在新行上顯示JSON結果AJAX

當時郵政編碼只是覆蓋彼此。

$.ajax({  
       url: 'process.php', 
       type: 'GET', 
       data: 'address='+ criterion, 
       dataType: 'json', 
       success: function(data) 
       { 
        jQuery.each(data, function() 
        { 
        $('#carParkResults').html("Postcode " + data[count].postcode); 
        codeAddress(data[count].postcode); 
        alert(count); 
        count++; 
        }); 
       }, 
       error: function(e) 
       { 
       //called when there is an error 
       console.log(e.message); 
       alert("error"); 
       } 
    }); 
+0

您可以爲每個郵政編碼創建一個新的'div'並將其追加。您還可以簡化您的'each'循環,查看文檔:http://api.jquery.com/jQuery.each/。 –

回答

2

使用append/appendTo可以將元素添加到DOM元素,而不會覆蓋元素的現有子元素。

$('<p>').text("Postcode " + data[count].postcode).appendTo('#carParkResults'); 
+0

乾杯隊友,我知道這將是簡單的事情。 – lnelson92

0

您需要使用.append(),.html()將始終替換div內的所有html。