Hi All,
I have a expense report form in which I'm adding up values from different tables and saving the result into a field outside the tables. With the example found in the help files I am able to do this without problems. However, I have a calculation that is not working, which is for getting the gas expense. This should calculated by multiplying the miles ran times a default value. I have added the function to do this, however it will not work on the form. This is the code I'm using:
$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
$('.miles').on('blur', 'input', calcMiles);
if ($('.subtotal').length > 0) {
$('.cf-table-block').on('blur', 'input', rowtotal);
}
function sumtotal() {
var sum = 0;
$('td.sum').each(function () {
var s = 0;
$(this).find('input').each(function () {
s += parseNumber($(this).val());
});
$(this).find('.subtotal input').val(s.toFixed(2));
$(this).find('.subtotal input').attr('readonly', 'True');
sum += s;
});
$('.total input').val(sum.toFixed(2));
$('.total input').attr('readonly', 'True');
}
function rowtotal() {
var sum = 0;
$('.cf-table-block tbody tr').each(function () {
var s = 0;
$(this).find('.sum input').each(function () {
s += parseNumber($(this).val());
})
$(this).find('.subtotal input').val(s.toFixed(2));
$(this).find('.subtotal input').attr('readonly', 'True');
sum += s;
});
}
function calcMiles() {
var sum = 0;
$('.cf-table-block tbody tr').each(function () {
var s=1;
$(this).find('.miles input').each(function () {
s *= parseNumber($(this).val());
})
$(this).find('.subtotal input').val(s.toFixed(2));
$(this).find('.subtotal input').attr('readonly', 'True');
sum += s;
});
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});
Here's a funny thing though:
If I change the trigger for the calcMiles function to this:
if ($('.miles').length > 0) {
$('.cf-table-block').on('blur', 'input', calcMiles);
}
That particular function works, but the rest of the code doesn't.
Any help will be greatly appreciated!