You are viewing limited content. For full access, please sign in.

Question

Question

Forms 10 Calculation Errors

asked on March 22, 2016

I have a form in version 10 doing some simple JavaScript calculations in two separate tables, the results of these calculation are displayed in a summary section where i have a new forms calculation which is simply minus one number from another and then multiply by 12, but all i get is this error below, if i change the 350 manually to say 400 the calculation works perfeclly as you can see below.

The calculation behind "Total Monthly Savings "on the form is

 

An example of the variables is below

All fields are number input boxes.

1 0

Answer

SELECTED ANSWER
replied on March 23, 2016 Show version history

Hi Ian,

At the end of line 14, add ".change()" right before the ending semicolon:

$('.grandTotal input').val(sum + parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val())).change();  

Do the same for line 37. This will trigger the inputs to "change" (as would happen if someone typed in the values), which does not happen when the value is inserted using JavaScript. This is necessary when you are passing off JavaScript calculation results to Forms calculations. 

1 0

Replies

replied on March 23, 2016

Hi Ian,

When you use JavaScript/jQuery to assign values to a field, such as $('#q1 input').val(someValue);, Forms does not register that the field has changed, and therefore does not start the calculation. In order for Forms to see the change, you must add a ".change()" event to the end of the statement:

$('#q1 input').val(someValue).change();​

Let me know if this works for you!

1 0
replied on March 23, 2016
$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
$('.tax').on('blur', 'input', sumtotal);
$('.shipping').on('blur', 'input', sumtotal);
function sumtotal() {
var sum = 0;
$('.cf-table-block tbody tr').each(function () {
var s = 0;
s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val());
$(this).find('.subtotal input').val(s);
sum += s; 
});
$('.total input').val(sum);
$('.grandTotal input').val(sum + parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val()));  
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});

// Second Inout Boxes

$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
$('.tax1').on('blur', 'input', sumtotal);
$('.shipping1').on('blur', 'input', sumtotal);
function sumtotal() {
var sum = 0;
$('.cf-table-block tbody tr').each(function () {
var s = 0;
s = parseNumber($(this).find('.price1 input').val()) * parseNumber($(this).find('.quantity1 input').val());
$(this).find('.subtotal1 input').val(s);
sum += s; 
});
$('.total1 input').val(sum);
$('.grandTotal1 input').val(sum + parseNumber($('.tax1 input').val()) + parseNumber($('.shipping1 input').val()));  
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});

Here's my JavaScript Alexanader, I'm no good with JavaScript, where would i incorporate the change() in ?

the variables names for the two Input boxes are "grandTotal" and "grandTotal1"

0 0
replied on March 23, 2016

That worked great

 

Thank you smiley

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.