正在使用JavaScript删除HTML表


Removing HTML Table with JavaScript

我有下面的代码,应该在页面加载后立即用数据库中的艺术家填充一个表(这很好)。然后,当搜索一个艺术家时,它应该显示匹配的艺术家,如果没有,它就会显示"没有找到艺术家!"(这很好)。问题是,它将新的表或消息放在原始表旁边,所以我添加了一些JavaScript来删除原始表,然后PHP回显新表或消息,这是不起作用的。

<table id="artistTable">
<!-- PHP to display default table of all artists -->
<?php
include 'connection.php';
$stmt = $conn->prepare("SELECT * FROM artist");
if(!$stmt)
    {
        echo "Error creating SQL statement";
        return 1;
    }
$stmt->execute();
$stmt->bind_result($artID, $artName);
echo "<tr>'n
        <th>Artist ID:</th>'n
        <th>Artist Name:</th>'n
        <th>Options:</th>'n
        </tr>'n";
while($stmt->fetch())
{
    echo "<tr>'n
            <td>" . htmlspecialchars($artID) . "</td>'n
            <td>" . htmlspecialchars($artName) . "</td>'n
            <td><a href='"'">Edit</a> - <a href='"'">Albums</a></td>'n
            </tr>'n";
}
?>
<!-- PHP to display table after search -->
<?php
include 'connection.php';
if(isset($_GET["artists"]))
{
    /* JavaScript to remove original table */
    echo "<script>
            var table = Document.getElementByID('"artistTable'");
            table.parentNode.removeChild;
            </script>";
    $artist = htmlspecialchars($_GET["artists"]);
    /* Add wildcards to $artist */
    $artist = "%$artist%";
    $stmt = $conn->prepare("SELECT * FROM artist WHERE (artName LIKE ?)");
    if(!$stmt)
    {
        echo "Error creating SQL statement";
        return 1;
    }
    $stmt->bind_param('s', $artist);
    $stmt->execute();
    $stmt->bind_result($artID, $artName);
    if($stmt->fetch())
    {
        echo "<tr>'n
                <th>Artist ID:</th>'n
                <th>Artist Name:</th>'n
                <th>Options:</th>'n
                </tr>'n";
        while($stmt->fetch())
        {
            echo "<tr>'n
                    <td>" . htmlspecialchars($artID) . "</td>'n
                    <td>" . htmlspecialchars($artName) . "</td>'n
                    <td><a href='"'">Edit</a> - <a href='"'">Albums</a></td>'n
                    </tr>'n";
        }
    }
    else
        echo "<p>No artists were found!</p>'n";
}
?>
</table>

试试这个$('#artistTable tbody').remove();然后$('#artistTable tbody').html('new table content');