我有一个表格,其中的列有两个输入字段。我想计算子行的第一个输入字段的总和,并将与列的父行的第一个输入字段对应的总数相加。
现在使用代码,它确实加起来正确,但它没有在父行的正确输入字段中设置总计(即在父行的相应输入字段中)。
请参阅jsFiddle。
代码:
HTML:
<table>
<tr class="parent-realtor percent-text">
<td>
<h5>Realtor Percent</h5>
</td>
<td>
<input type="text" class="percent-total" /> //parent
<input type="text" onfocus="this.blur()" class="percent-data" />
</td>
<td>
<input type="text" class="percent-total" /> //parent
<input type="text" onfocus="this.blur()" class="percent-data" />
</td>
<td>
<input type="text" class="percent-total" /> //parent
<input type="text" onfocus="this.blur()" class="percent-data" />
</td>
<td>
<input type="text" class="percent-total" /> //parent
<input type="text" onfocus="this.blur()" class="percent-data" />
</td>
</tr>
<tr>
<td>
<h6>Contract Amount</h6>
</td>
<td>
<input type="text" data-parent="realtor" />
</td>
<td>
<input type="text" data-parent="realtor" />
</td>
<td>
<input type="text" data-parent="realtor" />
</td>
<td>
<input type="text" data-parent="realtor" />
</td>
</tr>
<tr class="percent-text">
<td>
<h6>Buyer's Agent</h6>
</td>
<td>
<input type="text" data-parent="realtor" class="percent" /> //child
<input type="text" data-parent="realtor" class="percent-data r" onfocus="this.blur()" />
</td>
<td>
<input type="text" data-parent="realtor" class="percent" /> //child
<input type="text" data-parent="realtor" class="percent-data r" onfocus="this.blur()" />
</td>
<td>
<input type="text" data-parent="realtor" class="percent" /> //child
<input type="text" data-parent="realtor" class="percent-data r" onfocus="this.blur()" />
</td>
<td>
<input type="text" data-parent="realtor" class="percent" /> //child
<input type="text" data-parent="realtor" class="percent-data r" onfocus="this.blur()" />
</td>
</tr>
<tr class="percent-text">
<td>
<h6>Seller's Agent</h6>
</td>
<td>
<input type="text" data-parent="realtor" class="percent" /> //child
<input type="text" data-parent="realtor" class="percent-data r" onfocus="this.blur()" />
</td>
<td>
<input type="text" data-parent="realtor" class="percent" /> //child
<input type="text" data-parent="realtor" class="percent-data r" onfocus="this.blur()" />
</td>
<td>
<input type="text" data-parent="realtor" class="percent" /> //child
<input type="text" data-parent="realtor" class="percent-data r" onfocus="this.blur()" />
</td>
<td>
<input type="text" data-parent="realtor" class="percent" /> //child
<input type="text" data-parent="realtor" class="percent-data r" onfocus="this.blur()" />
</td>
</tr>
</table>
jQuery:
$('.percent').on('keyup', function () {
//calcRealtor();
var totals = [0, 0, 0, 0, 0, 0, 0, 0],
parent_name = $(this).data('parent'),
find_parent_row = $('tr.parent-realtor');
find_parent_row.nextUntil('[class="parent-realtor"]').find('input[data-parent="realtor"]').each(function () {
totals[$(this).parent('td').index() / 1 - 1] += +this.value;
});
find_parent_row.find('input').each(function (i) {
this.value = totals[i];
});
});