【问题标题】:How do I output the contents of an array in an html table while using jQuery?使用 jQuery 时如何在 html 表中输出数组的内容?
【发布时间】:2011-11-26 01:15:33
【问题描述】:

如何在使用 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);
}); 
});

【问题讨论】:

    标签: javascript jquery html arrays html-table


    【解决方案1】:

    据我所知,表格的中间不是&lt;div&gt; 标记的有效位置,这就是它没有显示在表格内的原因。为什么不把你的id 放在&lt;tbody&gt; 标签上,完全去掉 div?

    【讨论】:

    • 谢谢!我不知道可以在 tbody 标签中添加一个 id。我想我也可以添加一个类吧?
    • @JohnSmith 你可以添加idclasstitlestyle 属性到HTML 中的几乎任何东西,例外基本上只在&lt;head&gt;
    【解决方案2】:

    表格中不能有 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2012-08-11
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 2016-04-13
      • 1970-01-01
      相关资源
      最近更新 更多