在使用jQuery时,如何在HTML表中显示数组的内容?
这是我的脚本。这将在表的顶部输出数组中的对象,而不是表中的对象。
HTML
<table>
<thead>
<tr>
<th>ITEM ID</th>
<th>NUMBER OF BAGS</th>
<th>WEIGHT</th>
</tr>
</thead>
<tbody>
<div id="returnlist"></div>
</tbody>
</table>jQuery
var tempList = new Array();
$('#add').click(function(){
var split = $('#onhanditem').val().split('_');
var itemid = split['0'];
var kilo = $('#kilo').val();
var bagsReturned = $('#bagsReturned').val();
var totalbagkiloreturned = kilo+'_'+bagsReturned;
tempList[itemid] = totalbagkiloreturned;
list = '';
// i = ITEM ID | tempList = totalbagkiloreturned
for (var i in tempList){
var itemID = i;
var split = tempList[i].split('_');
var kilo = split['0'];
var numBags = split['1'];
list+='<tr><td>'+itemID+'</td><td>'+kilo+'</td><td>'+numBags+'</td></tr>';
}
$('#returnlist').html(list);
});
});发布于 2011-09-27 21:18:46
据我所知,表的中间位置不是<div>标记的有效位置,这就是为什么它不会显示在表中的原因。为什么不把你的div放在<tbody>标签上,完全去掉id呢?
发布于 2011-09-27 21:18:12
表中不能有div,它根本不是有效的HTML。
请尝试使用以下HTML
<table>
<thead>
<tr>
<th>ITEM ID</th>
<th>NUMBER OF BAGS</th>
<th>WEIGHT</th>
</tr>
</thead>
<tbody id="returnlist">
</tbody>
</table>https://stackoverflow.com/questions/7569750
复制相似问题