2012-05-09 51 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。