Arrays jQuery/json循环未使用.html()提供完整结果 //使用JSON调用视频函数 $.getJSON(“videos.php”,函数(数据){ //首先检查是否有可显示的成员, //如果没有,则显示错误消息 如果(数据=“”){ $('#tabs-4').html(“对不起,目前没有会员可用的视频”); } //如果有成员,则循环遍历每个可用数据 否则{ $.each(数据、函数(i、名称){ 内容=''; content+='

Arrays jQuery/json循环未使用.html()提供完整结果 //使用JSON调用视频函数 $.getJSON(“videos.php”,函数(数据){ //首先检查是否有可显示的成员, //如果没有,则显示错误消息 如果(数据=“”){ $('#tabs-4').html(“对不起,目前没有会员可用的视频”); } //如果有成员,则循环遍历每个可用数据 否则{ $.each(数据、函数(i、名称){ 内容=''; content+=',arrays,json,jquery,Arrays,Json,Jquery,'+name.name+''; 内容+=''; 内容+=''; $(“#tabs-4”).html(内容); }); } });​ 问题是它只给我一个结果,而不是数组中的结果列表,但是如果我附加到(内容)。。它在当前列表下添加完整的结果列表,这不是我想要的,因为我需要用更新的数据刷新该内容 你知道我做错了什么吗?如果我能很好地理解,你可能会想做这样的事情 // Calling the video function with JSON $.getJSON("videos.php", funct

'+name.name+'

'; 内容+=''; 内容+='

'; $(“#tabs-4”).html(内容); }); } });​ 问题是它只给我一个结果,而不是数组中的结果列表,但是如果我附加到(内容)。。它在当前列表下添加完整的结果列表,这不是我想要的,因为我需要用更新的数据刷新该内容


你知道我做错了什么吗?

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

// 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);
        });
    }
});​
。。。
否则{
$(“#tabs-4”).empty();//删除以前的数据(如果有)
$.each(数据、函数(i、名称){
内容='';
content+=''+name.name+'

'; 内容+=''; 内容+='

'; $(“#tabs-4”).append(内容);//追加新数据 }); }
填充前清空元件:

...
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
        });
}
$('#tabs-4').empty();
$.each(数据、函数(i、名称){
var含量=
'' +
“”+name.name+”

”+ '' + “

”; $(“#tabs-4”)。追加(内容); });
或者在将字符串放入元素之前将其放入字符串中:

$('#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内容=”;
$.each(数据、函数(i、名称){
内容+=
'' +
“”+name.name+”

”+ '' + “

”; }); $(“#tabs-4”).html(内容);
到目前为止,可能只显示最后一个元素

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);
//使用JSON调用视频函数
$.getJSON(“videos.php”,函数(数据){
//首先检查是否有可显示的成员,如果没有,则显示错误消息
如果(数据=“”){
$('#tabs-4').html(“对不起,目前没有会员可用的视频”);
} 
//如果有成员,则循环遍历每个可用数据
否则{
//如果要清除容器html
$(“#tabs-4”).html(“”);
$.each(数据、函数(i、名称){
内容='';
content+=''+name.name+'

'; 内容+=''; 内容+='

'; $(“#tabs-4”)。追加(内容); }); } });
Perfect。。。第二个是我使用的,因为我不想清空$(“#tabs-4”),只需要用内容更新它。非常感谢你是的你是对的。。。上面Guffa提供的第二个代码解决了这个问题。我不想清空$(“#tabs-4”)。。。但只需使用新内容更新它,因为我正在对此函数进行其他调用,所以.html()应该可以。谢谢你
// 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);
        });
    }
});