使用jQuery删除具有相同类名的多个表

Remove multiple tables with the same class name using jQuery

本文关键字:同类 jQuery 删除 使用      更新时间:2023-09-26

我正在尝试使用jQuery在一行中删除具有相同类名的多个表。

也许我在这里做错了什么,因为我不能让它工作。

JSFiddle


JavaScript:

$(".closeprod").live("click", function (e) {
    e.preventDefault();
    elem = $(this).parent().parent();
    //get sn            
    prodsn = $(".tbl").find(".prodsn:eq(0)");
    sn = $(prodsn[0]).html().substr(5);
    url = "delprod.asp?email=email@example.com&sn=" + sn + "&t=" + tpl;
    //remove product from xml file
    $.get(url, function (data, status) {
        if (data == "OK") {
            //remove product from template                  
            elem.remove();
        }
    });
});

使用closest()来获取父表而不是parent().parent(),因为更改html将破坏您的jquery代码:

elem = $(this).closest("table").closes("td");
代码:

$(".closeprod").live("click",function(e){
            e.preventDefault();
            elem = $(this).closest("table").closest("td");
                    //remove product from template                  
                    elem.remove();
        });

更新小提琴