我有一个表格,我想在列中减去值。但是,不要减去它不断累加的值。
HTML:
<table>
<tr class="parent-ca">
<td>
<h4><em>Contract Amount</em></h4>
</td>
<td>
<input type="text" class="rr" /> //all rr should be subtracted
</td>
<td>
<input type="text" class="rr1" /> //all rr1 should be subtracted
</td>
<td>
<input type="text" class="rr2" />
</td>
<td>
<input type="text" class="rr3" />
</td>
</tr>
<tr class="parent-abc">
<td>
<h5><em>Contract Amount</em></h5>
</td>
<td>
<input type="text" class="rr" />
</td>
<td>
<input type="text" class="rr1" />
</td>
<td>
<input type="text" class="rr2" />
</td>
<td>
<input type="text" class="rr3" />
</td>
</tr>
<tr class="parent-a">
<td>
<h5><em>Contract Amount</em></h5>
</td>
<td>
<input type="text" class="rr" />
</td>
<td>
<input type="text" class="rr1" />
</td>
<td>
<input type="text" class="rr2" />
</td>
<td>
<input type="text" class="rr3" />
</td>
</tr>
<tr class="parent-b">
<td>
<h5><em>Contract Amount</em></h5>
</td>
<td>
<input type="text" class="rr" />
</td>
<td>
<input type="text" class="rr1" />
</td>
<td>
<input type="text" class="rr2" />
</td>
<td>
<input type="text" class="rr3" />
</td>
</tr>
<tr class="parent-c">
<td>
<h5><em>Contract Amount</em></h5>
</td>
<td>
<input type="text" class="rr" />
</td>
<td>
<input type="text" class="rr1" />
</td>
<td>
<input type="text" class="rr2" />
</td>
<td>
<input type="text" class="rr3" />
</td>
</tr>
<tr class="nettotal tt table-sub" data-minus="ca,contract,other2">
<td>
<h4>Net Cash</h4>
</td>
<td>
<input type="text" id="rr" />
</td>
<td>
<input type="text" id="rr1" />
</td>
<td>
<input type="text" id="rr2" />
</td>
<td>
<input type="text" id="rr3" />
</td>
</tr>
</table>
jQuery的:
function calcSubMTotal(p) {
var minus = 0;
var minus_total = $('#' + p + '');
$('.' + p + '').each(function (i) {
if (!isNaN(this.value) && this.value.length !== 0) {
minus -= this.value; //trying to subtract but this just adds up rather than subtract
}
});
minus_total.val(minus);
}
$('input[type=text]').on('keyup', function () {
calcSubMTotal("rr");
calcSubMTotal("rr1");
calcSubMTotal("rr2");
calcSubMTotal("rr3");
});
答案 0 :(得分:1)
更新了FIDDLE
<强> Jquery的强>
function calcSubMTotal(p) {
var minus = 0;
var isFirst=true;
var minus_total = $('#' + p + '');
$('.' + p + '').each(function (i) {
if (!isNaN(this.value) && this.value.length !== 0) {
if(!isFirst)
minus -= parseInt(this.value);
else
{
minus=parseInt(this.value);
isFirst=false;
}
}
});
minus_total.val(minus);
}
$('input[type=text]').on('keyup', function () {
calcSubMTotal("rr");
calcSubMTotal("rr1");
calcSubMTotal("rr2");
calcSubMTotal("rr3");
});