在新行 AJAX 中显示 JSON 结果

Displaying JSON results in a on new lines AJAX

本文关键字:JSON 结果 显示 新行 AJAX      更新时间:2023-09-26

嗨,我正在使用JSON和ajax从数据库中检索一些数据。当我获取我需要的信息时,我希望它显示在div 中的新行上。

目前,邮政编码只是相互覆盖。

$.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");
              }
    });

使用 append/appendTo 将元素添加到 DOM 元素,而不覆盖元素的现有子元素。

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

使用 .append(),.html() 总是会替换div 中的所有 html。