如何将结果放入Json的DIV?

时间:2013-01-17 09:46:07

标签: javascript jquery json jquery-ui javascript-events

从下面的功能我得到的结果。现在我想把结果放入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;
            }
        }
    });

});

Thanx提前!!

4 个答案:

答案 0 :(得分: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 />");
       }

答案 1 :(得分: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);
       }


   }});

});

答案 2 :(得分: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>');

答案 3 :(得分: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 );
      });
}