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

Question

Question

Reference drop down selection in Forms javascript

asked on May 31, 2016 Show version history

I want this form to take information from the Expenses table based on the program selected, such as Mileage Reimbursement (this would be calculated with javascript), the Per Diem amount, and total.  Then, the Reimbursement by Program table would dynamically create rows based on programs selected in the Expenses table.  It would also dynamically fill in the mileage reimbursement, per diem, and total all per program.  Here is the Expense table, Reimbursement by Program table, and code.  I basically need help with the javascript coding to get this to work.  Thanks!

$(document).ready(function () {
    $('.amount').on('blur', 'input', 'change', sumtotal);
    $('.cf-table-block').on('blur', 'input', 'change', sumtotal);
  	$('.totalMiles').on('blur', 'input', 'change', sumtotal);
  	$('.regMiles').on('blur', 'input', 'change', sumtotal);
  	$('.filledField').on('blur', 'input', 'change', sumtotal);
  	$(this).find('.totalReim input').change(sumtotal);
  
    function sumtotal() {
      
        var sum = 0;
        var milesRe = 0; //mileage reim without fixed decimal
      	var n = 0; //use to fix 2 decimal places
      	var o = 0; //total without fixed decimal
        var p = 0; //use to fix 2 decimal places
      	var q = $(this).find('.filledField input').val();

      	var hdMiles = 0; //housing development miles
        var test = 0;
        $('.cf-table-block tbody tr').each(function () {
            var s = 0; //parsed per diem
            var totM = 0; //parsed total mileage
            var regM = 0; //parsed regular commute
          
            s = parseNumber($(this).find('.amount input').val());
          	totM = parseNumber($(this).find('.totalMiles input').val());
          	regM = parseNumber($(this).find('.regMiles input').val());
          
      	 	milesRe += (totM - regM) * .54;
          	n = milesRe.toFixed(2);
			sum += s;
          	o = sum + milesRe;
            p = o.toFixed(2);
            
            $('#q30 input').attr("readonly", true);
            $('#q15 input').attr("readonly", true);

 }); //end of .cf-table-block
        $('#q30 input').val(n);
        $('#q15 input').val(p);
    } //end of function
  
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

     

0 0

Answer

SELECTED ANSWER
replied on June 1, 2016

To begin with the first block of code needs to read:

$('.amount').on('blur input change', sumtotal);
$('.cf-table-block').on('blur input change', sumtotal);
$('.totalMiles').on('blur input change', sumtotal);
$('.regMiles').on('blur input change', sumtotal);
$('.filledField').on('blur input change', sumtotal);
$(this).find('.totalReim input').change(sumtotal);

As for the rest, not sure where "#q15" or "#q30" are cause I didn't see them there. My only other suggestion would be to move the lines:

$('#q30 input').attr("readonly", true);
$('#q15 input').attr("readonly", true);

outside of your table body loop. Hopefully that gets you pointed in the right direction. Let me know if I can assist you further.

0 0

Replies

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

Sign in to reply to this post.