jQuery each returns [object Object]

久未见 提交于 2019-12-12 16:29:03

问题


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);

回答1:


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();   



回答2:


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);


来源:https://stackoverflow.com/questions/9263813/jquery-each-returns-object-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!