javascript - jQuery each returns [object Object] - Stack Overflow

My problem is that the html variable returns something like this: [object Object][object Object][object

My problem is that the html variable returns something like this: [object Object][object Object][object Object][object Object][object Object], instead of the elements.

What should i do different?

var html = '';
$.each(data.response, function(index, value) { 
    var tr = $('<tr>');
    var tr_data = '<td>asd</td>';
    html += tr.data('trackinfo',value).html(tr_data);   
});

$(target).html(html);

My problem is that the html variable returns something like this: [object Object][object Object][object Object][object Object][object Object], instead of the elements.

What should i do different?

var html = '';
$.each(data.response, function(index, value) { 
    var tr = $('<tr>');
    var tr_data = '<td>asd</td>';
    html += tr.data('trackinfo',value).html(tr_data);   
});

$(target).html(html);
Share Improve this question edited Dec 22, 2015 at 20:08 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 13, 2012 at 16:11 passatgtpassatgt 4,4425 gold badges43 silver badges56 bronze badges 1
  • Can you please post what the data variable contains. I'm guessing JSON? – Rory McCrossan Commented Feb 13, 2012 at 16:13
Add a ment  | 

2 Answers 2

Reset to default 4

That's because you're setting the data on the tr and then filling it with your html, but still concatinating an object, which converts it to a string... aka

"[object Object]"

Not exactly sure what you're after but you might try changing this...

html += tr.data('trackinfo',value).html(tr_data);   

To this...

html += tr.data('trackinfo',value).html(tr_data).html();   

By default, Jquery creates objects not html mark-up. To get html you should to call html() method.

Here is working code:

var html = '';
$.each(data.response, function(index, value) { 
    var tr = $('<tr>');
    var tr_data = '<td>asd</td>';
    html += tr.data('trackinfo',value).html(tr_data);   
});

$(target).html(html);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742267433a4412067.html

相关推荐

  • javascript - jQuery each returns [object Object] - Stack Overflow

    My problem is that the html variable returns something like this: [object Object][object Object][object

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信