0

从下面的函数我得到了结果。现在我想将该结果放入一个 div 中。那么我应该如何将结果放入div中?

$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "../ajaxaction.php",
        data: {
            action: 'alllist'
        },
        dataType: 'json',
        success: function (msg) {
            for (var i = 0; i < msg.length; i++) {
                var uname = msg[i].user_name;
                var vtitle = msg[i].video_title;
                var vid = msg[i].video_id;
                var vthumb = msg[i].video_thumb;
            }
        }
    });

});
4

4 回答 4

2

您可以使用您喜欢的格式将结果附加到 div 中,但添加的简单方法是。您可以使用append()函数为循环的每次迭代保留添加的 html 和新的 html。

  success: function(msg){ 
       for (var i = 0; i < msg.length; i++) { 
        var uname = msg[i].user_name;
        var vtitle = msg[i].video_title;
        var vid = msg[i].video_id; 
        var vthumb = msg[i].video_thumb;
         $('#divId').append("Name: " + uname + "," + "Title: " + vtitle +
                            "Id: " + vid + "," + "Thumb: " + vthumb + "<br />");
       }
于 2013-01-17T09:49:16.633 回答
1
$(document).ready(function() { 
    $.ajax({
       type: "POST",
       url: "../ajaxaction.php",
       data: { action:'alllist'},
       dataType: 'json',
       success: function(msg){ 
       for (var i = 0; i < msg.length; i++) { 
        var uname = msg[i].user_name;
        var vtitle = msg[i].video_title;
        var vid = msg[i].video_id; 
        var vthumb = msg[i].video_thumb;

        $("#container").html("Username: " + uname + "<br />Video Title: " + vtitle + "<br />Vide ID: " + vid + "<br />Video Thumb: " + vthumb);
       }


   }});

});
于 2013-01-17T09:49:14.383 回答
1

如果您的 div 存在:

jQuery('div selector').append(uname+' '+vtitle+...whatever variable and format);

如果不

jQuery('element selector to put the div in').append('<div id="aID" class="some classes">'+uname+' '+vtitle+...whatever variable and format...+'</div>');

于 2013-01-17T09:49:30.810 回答
0

你可以这样做:

success: function(msg){ 
   $.each(msg, function(i, item){
      $('#divid').html('User Name : '+item.user_name+ 
                       'Video title : '+item.video_title+
                       'Video Id : 'item.video_id+
                       'Video Thumb : 'item.video_thumb );
      });
}
于 2013-01-17T09:54:36.803 回答