简体   繁体   English

无法使用Jquery删除表的tbody

[英]Unable to delete tbody for a table using Jquery

I have the following table inside my markup:- 我的标记里面有以下表格: -

<table id="tablelist" border="1">

       <thead>
        <tr>
    </tr>
    </thead>
       <tbody></tbody>
       </table>

and i am building the above table using the following Java script which will be fired when the user click on a link:- 我正在使用以下Java脚本构建上表,当用户单击链接时将触发该脚本: -

function getprocesslist(result) {
        $('#tablelist tbody').remove();

    var str = 'Total Number Of Processes:- ' + result.total.toString();
        $('#total').text(str.toString());

        var str1 = '<tr><th>' + 'Process Name' + '</th><th>' + 'Process Requestor ID' + '</th><th>' + 'Process State' + ' </th><th>' + 'Process Start Date' + ' </th><th>' + 'Process Due Date' + ' </th></tr>';
        $('#tablelist tr:last').after(str1);
    $.each(result.data, function (key, val) {
        var str2 = '<tr><td>' + val.name + '</td><td>' + val.requesterId + '</td><td>' + val.state + '</td><td>' + val.startedTime + '</td><td>' + val.due + '</td></tr>';

        $('#tablelist tr:last').after(str2);

    });
   }

But the problem is that when the user clicks multiple time on the link the new data will be shown under the old table instead of removing the old data. 但问题是,当用户点击链接上的多个时间时,新数据将显示在旧表下,而不是删除旧数据。 i think the problme with my code is that on the first clink the <tbody></tbody> is remove and i can not reference it again using Jquery ? 我认为我的代码的问题是,在第一个碰撞中, <tbody></tbody>被删除,我不能使用Jquery再次引用它? any idea on how i can solve this problem? 关于如何解决这个问题的任何想法?

BR BR

You are removing the tbody, then adding everything to the table head. 您正在删除tbody,然后将所有内容添加到表头。 Instead, empty the head and the body and append the content in the right places: 相反,清空头部和身体并将内容附加到正确的位置:

function getprocesslist(result) {
    $('#tablelist tbody, #tablelist thead').empty();

    var str = 'Total Number Of Processes:- ' + result.total.toString();
    $('#total').text(str.toString());

    var str1 = '<tr><th>' + 'Process Name' + '</th><th>' + 'Process Requestor ID' + '</th><th>' + 'Process State' + ' </th><th>' + 'Process Start Date' + ' </th><th>' + 'Process Due Date' + ' </th></tr>';
    $('#tablelist thead').append(str1);

    $.each(result.data, function (key, val) {
        var str2 = '<tr><td>' + val.name + '</td><td>' + val.requesterId + '</td><td>' + val.state + '</td><td>' + val.startedTime + '</td><td>' + val.due + '</td></tr>';
        $('#tablelist tbody').append(str2);
    });
}​

再次添加tbody。

$('#tablelist').append('<tbody></tbody>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM