JQuery 清除 HTML 表并插入新行 [英] JQuery clear HTML table and insert new rows
本文介绍了JQuery 清除 HTML 表并插入新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在四处寻找,但找不到有效的解决方案.
I have been scouting around but cannot find a solution that works.
我有一个简单的 HTML 表格:
I have a simple HTML table:
<table id="myTable">
<thead>
<tr>
<th>UserID</th>
<th>Current Submissions</th>
</tr>
<tbody>
<tr>
</tr>
</tbody>
<thead>
</table>
使用 socket.io 我正在接收 JSON 数据,我会在清除它后将其附加到表中.下面的代码只是删除表而不插入新数据?
using socket.io I am receiving JSON data that I would to append to the table, after clearing it. The code below just deletes the table and doesn't insert the new data?
<script>
var socket = io();
socket.on('totalsubmissions', function(msg) {
$('#myTable').find('tbody').remove();
// loop over each recevied in the object
$.each(msg, function(k, v) {
//display the key and value pair
var myRow = "<tr><td>" + k + "</td><td>" + v + "</td></tr>";
$('#myTable').find('tbody').append(myRow);
});
});
</script>
我是否错误地删除了表格?
Am I removing the table incorrectly?
推荐答案
这一行删除了 tbody
.
$('#myTable').find('tbody').remove();
所以当你到达这一行时:
So when you get to this line:
$('#myTable').find('tbody').append(myRow);
没有要查找的 tbody
.
变化:
$('#myTable').find('tbody').remove();
致:
$('#myTable').find('tbody').empty();
这篇关于JQuery 清除 HTML 表并插入新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文