如何在删除时删除行形式ajax

时间:2016-10-19 06:01:55

标签: php jquery ajax parent-child delete-row

enter image description here

你好我试图删除一个带复选框删除的行,当我删除它从数据库中删除的任何记录时,我可以看到刷新页面。

我想删除没有刷新的行如何实现: 这是我的上市代码:

<tbody>
    <?php while($staffResults=$staffQuery->fetch_assoc()){ ?>
        <tr>
            <td><input type="checkbox" class="staff-list" name="checked" value="<?php echo $staffResults['id_staff']; ?>"></td>
            <td><?php echo $staffResults['staff_name']; ?></td>
            <td><?php echo $staffResults['staff_email']; ?></td>
            <td><?php echo $staffResults['staff_phone']; ?></td>
            <td><?php  $datetime = $staffResults['staff_registered'];
            echo  date("d-m-Y", strtotime($datetime));   ?></td>
        </tr>
    <?php } ?>
</tbody> 

删除按钮:

<button type="button" id="deleteStaffList" class="btn btn-sm btn-default" title="Remove"><i class="fa fa-trash-o"></i></button>

这是我的脚本和ajax代码。

<script language="JavaScript">  
    $("#deleteStaffList").click(function()
    {
        $("#delshowMessageDiv").hide();
        $("#delshowMessage").html('');
        var deleteStaffList = $('.staff-list:checkbox:checked').map(function(){
            return $(this).attr('value');
        }).get().join(",");
        console.log(deleteStaffList); 
        $.ajax({
            url: "staffcontroller.php",
            method: "POST",
            data: { delData : deleteStaffList, 'action':'delete'},
            dataType: "json",
            success: function (response) {
                if(response["success"]==true)
                {
                    $("#delshowMessageDiv").hide();
                    $("#delshowSuccessMessageDiv").show();
                    $("#delshowSuccessMessage").html(response["message"]);
                    }else{
                    $("#delshowMessageDiv").show();
                    $("#delshowMessage").html(response["message"]);
                }   
            },
            error: function (request, status, error) {
                $("#delshowMessageDiv").show();
                $("#delshowMessage").html("OOPS! Something Went Wrong Please Try After Sometime!");
            }
        });
        return false;
    }); 
</script>

1 个答案:

答案 0 :(得分:3)

您可以按如下方式放置3行:(//新行-there comment- //直到此处)

    <script language="JavaScript">  
    $("#deleteStaffList").click(function()
    {
        $("#delshowMessageDiv").hide();
        $("#delshowMessage").html('');
        var deleteStaffList = $('.staff-list:checkbox:checked').map(function(){
            return $(this).attr('value');
        }).get().join(",");
        console.log(deleteStaffList); 
        $.ajax({
            url: "staffcontroller.php",
            method: "POST",
            data: { delData : deleteStaffList, 'action':'delete'},
            dataType: "json",
            success: function (response) {
                if(response["success"]==true)
                {
                    $("#delshowMessageDiv").hide();
                    $("#delshowSuccessMessageDiv").show();
                    $("#delshowSuccessMessage").html(response["message"]);
                    }else{
                    $("#delshowMessageDiv").show();
                    $("#delshowMessage").html(response["message"]);
                    // new lines
                    $('.staff-list:checkbox:checked').each(function(i,e){
                        $(this).parents('tr').remove();
                    })
                    // till here
                    }   
            },
            error: function (request, status, error) {
                $("#delshowMessageDiv").show();
                $("#delshowMessage").html("OOPS! Something Went Wrong Please Try After Sometime!");
            }
        });
        return false;
    }); 
</script>