0

我有一个克隆行的可编辑表,克隆的行在两列中都是可编辑的,带有文本区域输入数字。如果您点击编辑输入的数字,有一个功能可以自动计算总和并给出总金额。

我现在的问题是我无法正确配置克隆行的删除按钮。如果您尝试一下,您会发现它没有删除一行,而是增加了一行。请我需要帮助来解决这个问题。

这是克隆行的脚本,我认为它需要减少和改进

$('input:button').live('click',function(){
    var temprow = $('#temprow tbody').html();
    var tr = $(this).closest('tr');
    tr.after(temprow);
});

$("input.tr_clone_add").live('click', function() {
    var lastid = $('[id^="sum"]').length + 1;
    var $tr = $(this).closest('.tr_clone');
    var $clone = $tr.clone();
    $clone.find(':text').val('');
    $clone.attr('id', 'sum' + lastid)
    $tr.after($clone);
    initEditables();
    tally('p#subtotal');
    tally('p#total');
});

$(".tr_clone_remove").live("click", function() {
$(".tr_clone").last().remove();
    initEditables();
    tally('p#subtotal');
    tally('p#total');
});

initEditables();
tally('p#subtotal');
tally('p#total'); 

我的桌子

4

2 回答 2

1
  1. 将函数更改tr.after为:

    if ($(this).val() == '+') {
        tr.after(temprow);
    }
    
  2. 拨打remove()电话:

    $(this).parent().parent().remove();
    

这是小提琴

于 2012-10-19T06:41:57.880 回答
0

用正确的解决方案编辑了你的小提琴:http: //jsfiddle.net/Manna/S3kZw/5/

  • 由于tr_clone_remove该类也是按钮类型的输入,因此每次单击删除按钮时都会调用第一个处理程序。

    $('input:button').live('click',function(){
        var temprow = $('#temprow tbody').html();
        var tr = $(this).closest('tr');
        tr.after(temprow);
    });
    

    这就是插入行而不是删除行的原因。

  • 此外,使用正确的代码更新了您的删除行块。
于 2012-10-19T06:33:20.517 回答