簡體   English   中英

刪除表格行中的按鈕

[英]Remove button in table row

我正在開發購物車應用程序。 我已經使用jQuery來獲取項目並以表格格式顯示它。 每個項目表均包含一個刪除按鈕,可將其從購物車中刪除。 我已經使用桌子上的點擊功能刪除了該項目。 但是,如果我單擊qty字段,該表也會被刪除,因為它位於表中。 我希望在單擊表內的“刪除”按鈕時將其刪除。

$(document).ready(function(){
    $(".add_cart").click(function(){
        var id = this.id;
        $(this).prop("disabled",true);      
        $.get("cart.php",{
            "id":id
        },function(data){
            $("#sam").append("<table id='"+id+"' class='tables'><tr><td>"+data+"</td><td><input class='remove' id='"+id+"' type='button' value='Remove'></td></tr></table><br>");    
            $(".remove").click(function(){
                $(table.tables).remove();
                $("#"+id).prop("disabled",false);
            });
        });
    });
});

用這個:

$("table.tables").remove();

嘗試

$(document).ready(function () {
    $(".add_cart").click(function () {
        var id = this.id;
        $(this).prop("disabled", true);

        $.get("cart.php", {
            "id": id
        }, function (data) {
            $("#sam").append("<table id='" + id + "' class='tables'><tr><td>" + data + "</td><td><input class='remove' type='button' value='Remove'></td></tr></table><br>");
        });
    });
    //use event delegation
    $("#sam").on('click', '.remove', function () {
        //get the table which contains the button
        var $tbl = $(this).closest('tr').remove();
        //get the id of the table instead of the id variable
        $("#" + $tbl.attr('id')).prop("disabled", false);
    });
});

請注意,元素的ID必須是唯一的,您有一個表和一個具有相同ID的按鈕...從該按鈕中刪除該ID

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM