如何在不删除html表的情况下清空javascript

时间:2015-06-30 14:33:04

标签: javascript jquery html

我有一个html代码:

<form action="javascript:getAllRecords();" method="GET">
    <input type="submit" value="Show All" />
</form>
<table id="bordered-with-stripred-rows" class="table table-bordered table-hover table-striped">
    <thead>
        <tr>
            <th>Id</th>
            <th>Issue</th>
            <th>Description</th>
            <th>Input Date</th>
            <th>Edit Date</th>
            <th>Name</th>
            <th>Email</th>
        </tr>
    </thead>
</table>

和javascript:

function getAllRecords() {
    $.getJSON("getjson.php",
        function(response) {
            $("#bordered-with-stripred-rows").empty();
            var trHTML = '';
            $.each(response, function(key, value) {
                trHTML +=
                    '<tr><td>' + value.id +
                    '</td><td>' + value.issue +
                    '</td><td>' + value.description +
                    '</td><td>' + value.input_date +
                    '</td><td>' + value.edit_date +
                    '</td><td>' + value.name +
                    '</td><td>' + value.email +
                    '</td></tr>';
            });

            $('#bordered-with-stripred-rows').append(trHTML);
        });
}

我想要做的是在显示带有$("#bordered-with-stripred-rows").empty();代码的新记录之前我想要清空表格。但是当我在javascript代码中这样写时,它也会删除表并仅显示id,问题,描述等而不是表。我应该在哪里写空代码?

1 个答案:

答案 0 :(得分:2)

删除innerHTML的{​​{1}}。

tbody

$('#bordered-with-stripred-rows tbody').empty(); // OR html(''); 新的HTML append

tbody