jQuery清除HTML表并插入新行 [英] JQuery clear HTML table and insert new rows

查看:98
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆