在jQuery中删除选定的表行

时间:2016-05-11 09:20:18

标签: php jquery mysql

我是否知道如何仅删除所选行的表行。例如,如果我选择要删除的2行,则会删除所选行。我已经做了这个例子,但我不知道如何获得所选行的id。以下是示例

jQuery的:

$("#delete").click(function(){
        var del = $("#delete").val();
        var cb=[];
        $('.checkBox:checked').each(function(){
            cb.push($(this).val()); // id's of selected checkbox to delete
        });
        $.post('delete-pr.php',
        {
            del : del,
            cb : cb
        }, function(data){
            $("#result").html(data);
            $('#myTable tr:last').remove(); // this will delete last row only
        });  
});

HTML:

<table id="myTable">
<tr>
<th><input type="checkbox" name="selectAll" id="selectAll" title="Select All"></th>
<th>Title</th>
<th>Type of Purchase</th>
</tr>
<?php
$sql="SELECT * FROM purchase";
$result=mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0){
    while($row=mysqli_fetch_assoc($result)){
        $pr_id=$row['pr_id'];
?>
    <tr>
    <td align="center"><input type="checkbox" name="del[]" id="checkBox" class="checkBox" value="<?php echo $row['pr_id']; ?>"></td>
    <td><?php echo $row['title']; ?></td>
    <td><?php echo $row['purchase_type']; ?></td>
    </tr>
<?php
    } 
} 
?>
</table>

1 个答案:

答案 0 :(得分:1)

您可以在成功消息后添加

function(data){
    $("#result").html(data);
    $('.checkBox:checked').each(function(){
       $(this).parent('tr').remove() ; // id's of selected checkbox to delete
    });
});  
相关问题