从内部的链接中删除表格行

时间:2012-08-04 15:14:38

标签: jquery

我需要帮助使用jQuery删除选定的表行。

<tr data-file='+file_id1+'>
    <td width="40%">'+file_name1+'</td>
    <td>'+upload_date1+'</td>
    <td><a href="sym.php?doc_id='+file_id1+'" class="view2_fl">VIEW FILE</a> | <a href="javascript:void(0);" class="del2_fl">DELETE</a></td>
</tr>

当我点击DELETE链接时,文件将通过ajax从db中删除,如下所示: -

$('.addfile_popup').on('click', '.del2_fl', function(){
var job_id=$(this).data('job');
var file="file_id="+$(this).data('file')+"&job_id="+job_id;
$.ajax({
    type:"POST",
    url:"admin_includes/del_fl.php",
    data:file,

    success:function(html){
        if(html=="2")
        {
            //delete row

        }
    }
})//end ajax
});

我试图删除这一特定行导致我出现问题,因为我似乎无法使用'this'找到它。

感谢任何想法。

1 个答案:

答案 0 :(得分:0)

默认情况下,在success处理程序中,this绑定了$.ajaxSettings和您传递给$.ajax()的选项的混合。

您可以指定context选项将this绑定到您选择的对象,包括调用者中绑定的值this。从那里,您只需要closest()来找到要删除的表格行:

$.ajax({
    type: "POST",
    url: "admin_includes/del_fl.php",
    data: file,
    context: this,  // Relay 'this' to 'success' handler.
    success: function(html) {
        if (html == "2") {
            // Here, 'this' is the same as in the caller of '$.ajax()'.
            $(this).closest("tr").remove();
        }
    }
});
相关问题