我想將值保存到db中,我們計算稱爲total和grand total.so我想在輸入字段中計算它。我嘗試過但是不起作用。 當我意識到問題出現在$('.multTotal',this).text(total);
「文本」的行和$("#grandTotal").text(mult);
和「文本」的行中。 u能幫助我PLZjQuery不能在輸入字段中工作
查看
<table class="table" id="boq_tbl">
<thead>
<thead>
<tbody>
<tr class="txtMult">
<td><input type="text" name="work_product_id" class="form-control" id="work_product_id" placeholder=""></td>
<td><input type="text" name="cost_code" class="form-control" id="cost_code" placeholder=""></td>
<td><input type="text" name="work_item_description" class="form-control" id="work_item_description" placeholder=""></td>
<td><input type="text" name="quentity" id="" class="form-control val1" /></td>
<td><input type="text" name="unit" class="form-control val2"/></td>
<td><input type="text" name="laboure_hrs" id="" class="form-control val3" /></td>
<td><input type="text"name="laboure_cost" id="" class="form-control val4"/></td>
<td><input type="text" name="others" class="form-control" id="others" placeholder=""></td>
<td>
<span class="multTotal">0.00</span>
</td>
</tr>
</tbody>
</table>
<p align="right">
Grand Total# <span id="grandTotal">0.00</span>
</p>
jQuery的
<script>
$("#insert-more").click(function() {
$("#boq_tbl").each(function() {
var tds = '<tr class="txtMult">';
jQuery.each($('tr:last td', this), function() {
tds += '<td>' + $(this).html() + '</td>';
});
tds += '</tr>';
if ($('tbody', this).length > 0) {
$('tbody', this).append(tds);
} else {
$(this).append(tds);
}
});
});
$("#boq_tbl").on("keyup", ".txtMult input", multInputs);
function multInputs() {
var mult = 0;
// for each row:
$("tr.txtMult").each(function() {
console.log('---dkhal');
// get the values from this row:
var val1 = $('.val1', this).val();
var val2 = $('.val2', this).val();
var val3 = $('.val3', this).val();
var val4 = $('.val4', this).val();
var total = (val1 * val2) + (val3 * val4);
console.log('---'+val1 * val2);
$('.multTotal',this).text(total);
mult += total;
});
$("#grandTotal").text(mult);
}
</script>
不應該在添加和乘法之前解析這些值嗎? –
請試試我的回答 –