我想隐藏tr,但不起作用

时间:2018-08-03 23:27:55

标签: php jquery ajax codeigniter

这是 按下按钮删除时调用:

确认删除后,我想隐藏删除tr

function delete_confirmation(id) {
    var $tr = $(this).closest('tr');
    swal({
        title: "Are you sure?",
        text: "To delete this patient!",
        icon: "warning",
        buttons: true,
        dangerMode: true,
    })
    .then((willDelete) => {
        if (willDelete) {
            $.ajax({
                url: localStorage.getItem("base_url") + '' + 'index.php/Patient_ws/delete/',
                timeout: 4000,
                type: "POST",
                data: ({id: id}),
            }).done(function () {
                $tr.remove();
            })
            .fail(function () {
                swal({
                    title: ' erreur in server, try later...',
                    type: 'warning'
                })
            })
        } else {
            swal("Patient not deleted");
        }
    });
}

HTML代码:

<a href="#" class="btn btn-danger btn-xs" onclick="delete_confirmation(<?php echo $patient->id_personne;?>)">
<i class="fa fa-trash-o"></i> Delete </a>

我想隐藏tr,请帮忙。

1 个答案:

答案 0 :(得分:1)

this函数中没有明确的onclick,除非您从html传递了它。尝试console.log(this)会发现它可能是Window而不是元素

由于您已经在使用jQuery,因此建议您也将其用于事件监听器,并将php输出移至数据属性

HTML

<a class="delete-row-btn btn btn-danger btn-xs" 
   data-id="<?php echo $patient->id_personne;?>">

JS

$(document).on('click','.delete-row-btn', delete_confirmation)

function delete_confirmation(event) {    
        event.preventDefault();
        // now `this` is the button
        var id = $(this).data('id')
        var $tr = $(this).closest('tr');
        // all else should be same
        swal({
              .....

}