我需要在我的PHP中顯示JSON數據。數據將作爲一個循環來使用。原始數據看起來是這樣的:在循環中顯示JSON數據
[{
"count": 3,
"not": "New Intender details entered"
},{
"count": 3,
"not": "New Intender details entered"
},{
"count": 3,
"not": "New Intender details entered"
}]
我的AJAX腳本如下:
function addmsg(type, msg) {
var obj = jQuery.parseJSON(msg);
$.each(obj, function(index, value) {
var count = value.count;
var not = value.not;
alert(not);
$('#msg_count').html(count);
$('#notification').html(not);
});
}
function waitForMsg(){
$.ajax({
type: "GET",
url: "notification/select.php",
cache: false,
timeout: 50000,
success: function(data){
addmsg("new", data);
setTimeout(waitForMsg, 1000);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(waitForMsg, 15000);
}
});
};
$(document).ready(function(){
waitForMsg();
});
我的HTML如下:
<span id="msg_count"></span>
<span id="notification"></span>
我現在面臨的問題是,msg_count
是顯示很好。現在通知是一個循環,這意味着它包含多個記錄。當我通過span
調用它時,它只顯示一條記錄。其餘的記錄沒有顯示。我給出了一個提醒所有記錄的alert(not)
。我只有在HTML中顯示爲<span id="notification"></span>
的問題。誰能幫我?
你試過調試過嗎?當你在'$ .each'循環中,並且當你第一次遍歷它時,'$('#notification')'具有「not」值嗎?在循環迭代三次之後,'html'函數似乎覆蓋了跨度的前一個值。 – yanis