【问题标题】:jQuery: hide table rowjQuery:隐藏表格行
【发布时间】:2018-08-03 23:27:55
【问题描述】:

这是按下按钮删除时调用的函数。确认删除后,我想隐藏删除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 - 请帮忙。

【问题讨论】:

  • 请添加一些细节以澄清“不起作用”。发生什么了?没有?不是你想要的东西?错误?
  • 删除请求工作。但确认后 没有被删除
  • 我明白了。所以&lt;a&gt;onclick="delete_confirmation()" 在你想被删除的&lt;tr&gt; 里面,对吧?只要确保
  • 是的,包含我按下的按钮删除的 tr 将在确认删除操作后被删除
  • 当我写 console.log(this): messege:Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}

标签: php jquery ajax codeigniter


【解决方案1】:

onclick 函数中没有显式的this,除非您从 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({
              .....

}

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签