删除html元素仍会显示无值行

时间:2017-08-07 02:43:22

标签: javascript php jquery laravel

我在这里用jquery编写的代码删除了一行数据。问题是当我删除该行时,它仍然显示一个空行。我怎么能删除那个空行?

            $(document).on('click','.btn_remove', function(){
            a++;                
            var button_id = $(this).attr("id");  
            var deletedItem = $('#total'+button_id+'').text();
            var getTotal = $('#samp').val();
            a = parseFloat((getTotal) - (deletedItem));
            $('#total_pay').text(a);
            $('#samp').val(a);
            $('#inc').val(i);
            $('#row'+button_id+'').remove();               
            }); 
            });

这是我生成HTML值的代码:

       $('#add').click(function(){
        i++;
        var item_name = $('#dummy').val();
        var price = $('#getRequest').val();
        var quantity = $('#sale_qty').val();
        var total = price * quantity;
        $('.dynamic_field').append(
            '<tr id="row'+i+'"><td class="total1">'+item_name+'</td><td class="total2">'+quantity+'</td><td id="total'+i+'" class="total" value="'+total+'">'+total+'</td><td><button type="button" class="btn btn-danger btn_remove" name="remove" id="'+i+'">X</button></td><td><input type="hidden" name="i_name'+i+'" id="name'+i+'"><input type="hidden" name="quan'+i+'" id="quan'+i+'"><input type="hidden" name="tot'+i+'" id="tot'+i+'"></td></tr>');
        $("#getRequest").val(" ");
        $('#product_category').prop('selectedIndex',0);
        $('#prod_name').prop('selectedIndex',0);
        $('#sale_qty').val(" ");
        });

HTML:

                    <label>Registered Items:</label>
                    <table class="table dynamic_field">
                    <thead>
                    <th>Item Name</th>
                    <th>Quantity</th>
                    <th>Total</th>
                    <th>Status</th>
                    </thead>   
                    <tbody>
                    <!--empty-->

                    </tbody>
                    </table>
                    <label>TOTAL:&nbsp;<span id="total_pay" name="total_payment">0</span></label>
                    </div>
                    </div>

1 个答案:

答案 0 :(得分:0)

试试这个

$('#row'+button_id+'').closest('tr').remove();  

我希望它有效