// Calling the video function with JSON
$.getJSON("videos.php", function(data){
// first check if there is a member available to display,
//if not then show error message
if(data == '') {
$('#tabs-4').html("<div class='errorMember'>Sorry, there is currently no member available videos</div>");
}
// if there is a member, then loop through each data available
else {
$.each(data, function(i,name){
content = '<div class="left"><img src="' + name.pic + '"/>';
content += '<p>' + name.name + '</p>';
content += '<a href="' + name.link + '" target="_blank">Video link</a>';
content += '</div><br/><hr>';
$("#tabs-4").html(content);
});
}
});问题是它只给我一个结果,而不是数组中的结果列表,但是如果我appendTo(content) ..它在当前下添加了完整的结果列表,这不是我想要的,因为我需要用更新后的数据刷新该内容。
你知道我做错了什么吗?
发布于 2012-08-30 18:44:27
在填充之前清空元素:
$('#tabs-4').empty();
$.each(data, function(i,name){
var content =
'<div class="left"><img src="' + name.pic + '"/>' +
'<p>' + name.name + '</p>' +
'<a href="' + name.link + '" target="_blank">Video link</a>' +
'</div><br/><hr>';
$("#tabs-4").append(content);
});或者在将字符串放入元素之前将所有元素放入其中:
var content = '';
$.each(data, function(i,name){
content +=
'<div class="left"><img src="' + name.pic + '"/>' +
'<p>' + name.name + '</p>' +
'<a href="' + name.link + '" target="_blank">Video link</a>' +
'</div><br/><hr>';
});
$("#tabs-4").html(content);发布于 2012-08-30 18:52:34
到目前为止,可能只显示了最后一个元素。
// Calling the video function with JSON
$.getJSON("videos.php", function(data){
// first check if there is a member available to display, if not then show error message
if(data == '') {
$('#tabs-4').html("<div class='errorMember'>Sorry, there is currently no member available videos</div>");
}
// if there is a member, then loop through each data available
else {
//If you want to Clear the Container html
$("#tabs-4").html('');
$.each(data, function(i,name){
content = '<div class="left"><img src="' + name.pic + '"/>';
content += '<p>' + name.name + '</p>';
content += '<a href="' + name.link + '" target="_blank">Video link</a>';
content += '</div><br/><hr>';
$("#tabs-4").append(content);
});
}
});发布于 2012-08-30 18:37:42
如果我很理解的话,你可能想做这样的事情。
...
else {
$("#tabs-4").empty(); // remove previous data (if any)
$.each(data, function(i,name){
content = '<div class="left"><img src="' + name.pic + '"/>';
content += '<p>' + name.name + '</p>';
content += '<a href="' + name.link + '" target="_blank">Video link</a>';
content += '</div><br/><hr>';
$("#tabs-4").append(content); // append new data
});
}https://stackoverflow.com/questions/12194916
复制相似问题