我从输入框中的数据库中获取值,然后添加值。有一些输入字段可以扣除这些值;我已经编写了代码,但它没有工作,它只给了我总和值,但没有做减法。请帮助我。
<script type ="text/javascript">
$(document).ready(function(){
//iterate through each textboxes and add keyup
//handler to trigger sum event
$(".text").each(function() {
$(this).on('input',function(){
calculateSum();
});
});
// call calculateSum function after typing in subtract field
$('.sub').on('input',calculateSum);
});
// function for subtracting from total
function totalSubtract(sumTotal) {
var subtract = parseFloat($('.sub').val());
sumTotal -= subtract;
return sumTotal;
}
function calculateSum() {
//console.log('calc');
var total = 0;
//iterate through each textboxes and add the values
$(".text").each(function() {
//add only if the value is number
if(!isNaN(this.value) && this.value.length!=0) {
total += parseFloat(this.value);
}
});
// subtract from total
total = totalSubtract(total);
//.toFixed() method will roundoff the final sum to 2 decimal places
$("#amount").val(total.toFixed(2));
}
</script>
&#13;