0
// 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) .. 它会在 current 下添加完整的结果列表,这不是我想要的,因为我需要刷新该内容与更新的数据。

对我做错了什么有任何想法吗?

4

3 回答 3

1

到目前为止,可能只有最后一个 Element 正在显示。

// 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-30T10:52:34.363 回答
0

如果我很好理解,可能你可能想做这样的事情

...
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
        });
}
于 2012-08-30T10:37:41.847 回答
0

在填充之前清空元素:

$('#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-30T10:44:26.707 回答