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

Question

Question

Form to calculate default values

asked on September 19, 2014

 Hello,

 

I am using your code in the help files to add few fields. That is working well. But I need it to add even when default values are assigned to the fields , so that the user sometimes need not need have to worry about typing a value.

 

This is the current code I am using.  

 

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

0 0

Replies

replied on September 19, 2014 Show version history

If there are values when the form loads, you should call the sumtotal function when the document loads.

 

$(document).ready(function () {
    sumtotal();
    $('.sum').on('blur', 'input', sumtotal);

    function sumtotal() {
        var s = 0;
        $('.sum input').each(function () {
            s += parseNumber($(this).val());
        });
        $('.total input').val(s);
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

 

 

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

Sign in to reply to this post.