每次从 PHP 文件返回数据时,似乎 json 对象都会增加一倍!?当我第一次运行脚本时,我得到 5 行,这与表中的行数相同。但第二次,我得到了两次结果!我的脚本中做错了什么?
function readData() {
$.ajax({
url: "read.php",
type: "POST",
dataType: "json",
data: {
input: 1
},
cache: false,
success: function(data) {
var html = "";
for (i = 0; i < data.length; i++) {
html += "<tr><td>" + data[i].text + "</td></tr>";
}
$(".contentList table").append(html);
},
});
}
最佳答案
尝试改变
$(".contentList table").append(html);
至
$(".contentList table").html(html);
.append()
只会将更多结果附加到标记内内容的末尾,而 .html()
将完全替换内容。
关于javascript - Json 对象每次返回时都会加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48281154/