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

Question

Question

javascript help

asked on August 13, 2015

can somebody provide me with javacript for a calculation for a form i am doing.  i need to be able to calculation 3 fields.  below is an example.

(# of buses) * (mileage) * ($2.50 bus rate)

fields would be the same in the form, number of buses, mileage, bus rate.  it would help me out a lot for someone to help me figure out this script.  thanks in advanced.

0 0

Replies

replied on August 13, 2015 Show version history
 
 

The general format will look like Eric's post over here.

 

You'll have to adapt it to fit what the names or classes of your input and output fields are.

 

The general schematic of your script will be

$(document).ready(function() {
  
  $(' .busfair').on('blur', 'input', gettotal);
                    
  function gettotal(){
  	var buscount = $('.buscount input').val();
  	var miles = $('.totalmiles input').val();
  	var rate = $('.busfair input').val();
  
  	var total = buscount * miles * rate;
  
  	$('.total input').val(total);

  }
});
  

For fields setup like so, note the css classes assigned to each field.

 

The total will populate as soon as the "bus rate" field loses focus

 
 
2 0
replied on August 13, 2015

Here's a sample form:

The four fields are just #q1, #q2, #q3, and #q4 respectively. In the form editor, I set the "Bus Rate" field to be read-only with a default value of 2.50.
Here's the Javascript:
 

$(document).ready(function () {
  $('#q4 input').attr('readonly',true);
  $(document).on('change', '#q1 input', producttotal);
  $(document).on('change', '#q2 input', producttotal);
  
  function producttotal() {
    var s = parseFloat($('#q1 input').val() * $('#q2 input').val() * $('#q3 input').val());
    $('#q4 input').val(s.toFixed(2));
  }
  
  function parseNumber(n) {
    var f = parseFloat(n); //Convert to float number.
    return isNaN(f) ? 0 : f; //treat invalid input as 0;
  }
});
2 0
replied on August 13, 2015

both work perfectly!  thank you very much.

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

Sign in to reply to this post.