Jquery隐藏按钮按下表格中的行

时间:2016-07-05 07:40:25

标签: php jquery

我有以下表格代码:

echo "<table border='1'>
        <tr>
            <th>Code</th>
            <th>Name</th>
            <th>Price</th>
        </tr>";

$sql = "SELECT * FROM products where updated_account_ID='$aktiv'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['code'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['price'] . "</td>";
    echo "<td>" . "<button name='code' value='".$row['code'] ."'>Delete</button></td>";

echo "</tr>";

我想在按下“删除”按钮后使用jquery隐藏或删除指定的行,该按钮也会删除数据库中的记录。有人可以这么善良,帮助做到这一点吗? 谢谢!

1 个答案:

答案 0 :(得分:-1)

jQUERY:

$(".delete").click(function(){
var product_id = $(this).parents('tr').attr('data-product-id');
$(this).parents('tr').remove();
$.ajax({
    url: "remove-record.php",
    data: product_id,
    type: "post",
    success: function(result){
        alert(result);
    }
});

});

PHP文件:

echo "<table border='1'>
    <tr>
        <th>Code</th>
        <th>Name</th>
        <th>Price</th>
    </tr>";

$sql = "SELECT * FROM products where updated_account_ID='$aktiv'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<tr data-product-id='".$row['id']."'>";
echo "<td>" . $row['code'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . "<button name='code' value='".$row['code'] ."'>Delete</button
</td>";

echo "</tr>";

remove-record.php:

$product_id = $_POST['data'];
$sql = "DELETE FROM products WHERE id=product_id";
echo json_encode('Record deleted');