我正在尝试迭代同一td标签中的数组 我尝试这样做:
$.each(data, function(index, val){
row.append($("<td>" + val.attr_value; + "</td>"));
});
但是它正在循环打印两个td
标签,但是我需要在单个td
标签中迭代并打印值
答案 0 :(得分:1)
首先在您的迭代块中生成数据:
$.each(data, function(index, val){
//Concatenate data here
});
然后将生成的data
插入TD并将其附加到所需内容:
row.append($("<td>" + data ));
答案 1 :(得分:1)
here is an example how to add td
with values programmatically
let data = [1, 2]
let $td = ''
$.each(data, function(index, val) {
$td += '<td>' + val + '</td>'
})
$('table tbody tr').append($td)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>first column</th>
<th>second column</th>
</tr>
</thead>
<tbody>
<tr></tr>
</tbody>
</table>
答案 2 :(得分:0)
If you want everything from the data
array inside a single <td>
you need to move the creation of the td
outside your loop, and let the loop simply build your content as a string. Something like this:
var content = "";
$.each(data, function(index, val){
content += val.attr_value + " ";
});
row.append($("<td>" + content + "</td>"));