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

Question

Question

Help Calculating Checkbox Value and Radio Value

asked on October 3, 2014 • Show version history

We are working on a Travel Reimbursement form using Forms 9.2. We have section called Meals that looks like the following:

The "Less than 12 hours" option has a value of 0. The "Over 12 hours" option has a value of 35. The Breakfast, Lunch, and Dinner options each have their own values as well. I have been using the following code to try and calculate the "Hours" value with any or all of the "Meals" values that are selected, but I can only get it to grab the "Hours" value.

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

The Meals field and the Hours field both have the mealsum CSS class assigned to the. The 1 Day Meal Total field has the mealtotal CSS class assigned.

Any ideas of what needs to be changed?

1 0

Answer

SELECTED ANSWER
replied on October 3, 2014 • Show version history
$(document).on('blur change', '.mealsum input', summealtotal);
  
    function summealtotal() {
        var s = 0;
      $('.mealsum input:checked').each(function () {
            s += Number($(this).val().replace(/V_/g,''));
        });
        $('.mealtotal input').val(s);
    }
  

The checkbox value gets a "V_" appended to it, so you need to remove that.

 

0 0
replied on October 3, 2014

That works great. I did notice however that if I select the checkboxes and then uncheck one it does not alter the mealtotal. Is there a way to do that?

1 0
replied on November 3, 2014 • Show version history

I had the same issue and used a conditional statement to create the two separate situations:

$('#q29 table tbody tr').each(function () {

if ($(this).find('.App input').is(':checked') == true){

//...calculations when box is checked

} else {

//...calculations when box is unchecked

}

That allowed the result to update in both cases. Hope it helps!

 

1 0

Replies

replied on October 3, 2014

Might wanna try something like this for the Checkbox. The radio and checkbox fields cannot be referenced the same:

var checkedValue = $('.Meals:checked').val();

 

0 2
replied on October 3, 2014

That's not true.

3 0
replied on October 3, 2014

Hadnt tested it, I have normally gotten the true or false of a field being checked through direct field references with a hyphen.

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

Sign in to reply to this post.