在相应的输入字段中添加和设置总值

时间:2013-12-08 03:03:27

标签: jquery html jquery-calculation

我正在尝试在一列中添加子行中的值,这应该在该列的父行中设置总计。每个子td都应在相应的父td中添加值。

现在正在发生的事情是,我可以在第一列中添加子行的值,并在父行的相应输入字段中设置总计;如果我然后在第二列中添加子行的值,它不会在第二列的父级中设置值。它累加到第一列的父级。

以下是代码 [jsFiddle]

HTML:

<table class="table">
<tr class="parent-A">
        <td><h5>A</h5></td>
        <td>&nbsp;</td>
        <td><input type="text" /></td>
        <td>&nbsp;</td>
        <td><input type="text" /></td>
        <td>&nbsp;</td>
        <td><input type="text" /></td>
    </tr>
    <tr>
        <td><h6>A1</h6></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="A" /></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="A"  /></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="A"  /></td>
    </tr>
    <tr>
        <td><h6>A2</h6></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="A" /></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="A"  /></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="A"  /></td>
    </tr>
    <tr>
        <td><h6>A3</h6></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="A" /></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="A"  /></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="A" /></td>
    </tr>
    <tr class="parent-B">
        <td><h5>B</h5></td>
        <td>&nbsp;</td>
        <td><input type="text" /></td>
        <td>&nbsp;</td>
        <td><input type="text" /></td>
        <td>&nbsp;</td>
        <td><input type="text" /></td>
    </tr>
    <tr>
        <td><h6>B1</h6></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="B" /></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="B"  /></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="B"  /></td>
    </tr>
    <tr>
        <td><h6>B2</h6></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="B" /></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="B"  /></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="B"  /></td>
    </tr>
    <tr>
        <td><h6>B3</h6></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="B" /></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="B"  /></td>
        <td>&nbsp;</td>
        <td><input type="text" data-parent="B" /></td>
    </tr>
</table>

jQuery的:

$('input[type=text]').on('blur', function() {

    var $dataRows=$(".table");
    var parent_name = $(this).attr('data-parent');
    var find_parent_row = $('tr.parent-' + parent_name);
    var $this_row = $(this).closest('tr');

        $this_row.each(function() {
            $(this).find('input').each(function(i){        
                totals[i]+=parseInt( $(this).val());
                console.log(totals[i]);
                var eVal = (isNaN(totals[i])) ? 0 : totals[i];
                $(find_parent_row).find('input').eq(i).val(eVal);
            });

        });

    });



});

1 个答案:

答案 0 :(得分:1)

尝试

fiddle Demo

$(function () {
    $('[class*="parent-"] input').attr('readonly', true);
    $('input[type=text]').on('blur', function () {
        var totals = [0, 0, 0];
        var $dataRows = $(".table");
        var parent_name = $(this).data('parent');
        var find_parent_row = $('tr.parent-' + parent_name);
        find_parent_row.nextUntil('[class*="parent-"]').find('input').each(function () {
            totals[$(this).parent('td').index() / 2 - 1] += +this.value;
        });
        find_parent_row.find('input').each(function(i) {
            this.value = totals[i];
        });
    });
});