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

Question

Question

field values copy to other fields in calculations

asked on November 28, 2017 Show version history

 

I have included code and a copy of the form fields I am wanting to copy to other form fields and use the values in calculations.  Any advice would be appreciated.  

 

//Table to calculate meals estimate (var=sub;B=10,L=20,D=30)
$(document).ready(function(){
  $('.cf-table-block').on('blur', 'input', sumtotal);
  function sumtotal(){
    $('.cf-table-block tbody tr').each(function(){
      var sub = 0;
      var col1 = parseNumber($(this).find('.col1 input').val());
      var col2 = parseNumber($(this).find('.col2 input').val());
      var col3 = parseNumber($(this).find('.col3 input').val());
      sub = col1*10 + col2*20 + col3*30;
      $(this).find('.sub input').val(sub);
    });
  }
  function parseNumber(n){
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
  }
});

//Table calculation for Private Car (var=privC)
$(document).ready(function() {
    $('.mileage input').attr('pattern', '\\d{1,9}?([.]\\d{1,2})');
	$('.rate input').attr('pattern', '\\d{1,9}?([.]\\d{1,3})');
  	$('.cf-table-block').on('blur', 'input', sumtotal);
	$('.tax').on('blur', 'input', sumtotal);
	$('.shipping').on('blur', 'input', sumtotal);
    $('.mileage input').attr("step", '0.001');
    $('.cf-table-add-row').click(function(){
    $('.mileage input').attr("step", '0.001');
    });
  
	function sumtotal() {
		var sum = 0;
		$('.cf-table-block tbody tr').each(function () {
          	var privC = 0.0;
			privC = parseFloat($(this).find('.mileage input').val()) * parseFloat($(this).find('.rate input').val());
          	privC = Math.round(privC * 100)/100.0; //round to two decimal places
            $(this).find('.subtotal input').val(privC);
            sum += privC;
            //sum += parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val());
            			
		});
			$('.pctotal input').val(sum);
	}
	function parseNumber(value) {
		var f = parseFloat(value); //Convert to float number
		return isNaN(f) ? 0 : f; //treat invalid input as 0;
	}
});

//Table Calculation and transTotal
$(document).ready(function () {
    $('.cf-table-block').on('blur', 'input', sumtotal);
    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);
            sum += s;
        });
        $('.transTotal input').val(sum);
    }
    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);
            sum += s;
        });
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

//Calculate Expense totals (var=tots)
$(document).ready(function () {
	$('.sum').on('blur', 'input', sumtotal);
	function sumtotal() {
	var tots = 0;
	$('.sum input').each(function () {
		tots += parseNumber($(this).val());
		});
	$('.total input').val(tots);
	}
function parseNumber(n) {
	var f = parseFloat(n); //Convert to float number.
	return isNaN(f) ? 0 : f; //treat invalid input as 0;
	}
});

//copy fields values to other fields
$(document).ready(function () {
  alert('hello world')
  $('.sub, .transTotal, .pctotal input').on('change', function() {
    alert('hello world')
    //Meals Estimate (B/L/D)
  	$('[name="Field22(1)"]').val($('.sub input').val());
    //Private Car Mileage Estimate Total
    $('[name="Field86(1)"]').val($('.pctotal input').val());
    //Transport Estimate Total copy to Transportation
    $('[name="Field20(1)"]').val($('.transTotal input').val());
  });
});

 

0 0

Replies

replied on November 28, 2017

Is the code you provided relevant to your form/question? If you want to copy a value from point A to B you can use JavaScript or, more simply, calculations. See here.

 

0 0
replied on November 28, 2017 Show version history

The Portion at the bottom of the code block is the javascript that should work.  I have tried it and nothing happens.  

//copy fields values to other fields
$(document).redy(function () {
  $('.sub, .transTotal, .pctotal input').on('change', function() {
    //Meals Estimate (B/L/D)
    $('[name="Field22(1)"]').val($('.sub input').val());
    //Private Car Mileage Estimate Total
    $('[name="Field86(1)"]').val($('.pctotal input').val());
    //Transport Estimate Total copy to Transportation
    $('[name="Field20(1)"]').val($('.transTotal input').val());

  });
});

 

0 0
replied on November 29, 2017 Show version history

Typo in this line:

$(document).redy(function () {

You can check the console (CTRL + SHIFT + J on Windows for Chrome) when viewing your form's preview, which will catch any trivial errors like this.

 

0 0
replied on November 29, 2017

Thank you for pointing that typo out; corrected and still no change.  I made sure I was calling the right variables and had the right field names also.  

0 0
replied on November 29, 2017
$('[name="Field22(1)"]').val($('.sub input').val());

This could also be the issue. Does this work?

$('#q22 input').val($('.sub input').val());

I also like to throw in an alert to make sure the function actually gets triggered. e.g.

 alert('hello world');

 

0 0
replied on November 29, 2017

added above  as requested.  The ready function loads because the alert appears, however the change function may not be loading because the alert does not appear.  

 

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

Sign in to reply to this post.