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

Question

Question

JQuery: How can I get my form to update the calculation when I click my radio button.

asked on June 19, 2015

I click a radio button which updates a hidden field with a value for a Tax Rate based on the province selection.

// Set Tax Rate
$(document).ready(function() {

  $('.province input').on('click', function() {

     $('.taxrate input').val($(this).val()).trigger('change');

  });

});

 

I then use the following code ( I realize this may not be the most efficient method but with limited JQuery experience I essentially modified one of your code examples to get this to work) to calculate the Tax based on the chosen rate.

 

// Calculate the Tax
$(document).ready(function () {
    $('.cf-table-block').on('blur', 'input', sumtotal);
    $('.taxrate').on('blur', 'input', sumtotal);
    $('.subtotal').on('blur', 'input', sumtotal);
    $('.province').on('blur', 'input', sumtotal);
    function sumtotal() {
        var sum = 0;
        $('.cf-table-block tbody tr').each(function () {
//            var s = 0;
//            s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val());
//            $(this).find('.subtotal input').val(s);
//            sum += s;
            sum += parseNumber($('.taxrate input').val()) * parseNumber($('.subtotal input').val());
        });
        $('.tax input').val((sum).toFixed(2));
    }
    function parseNumber(n) {
        var f = parseFloat(n); //Convert to float number.
        return isNaN(f) ? 0 : f; //treat invalid input as 0;
    }
});

 

I added this line above the the Tax Rate will calculate when the Province radio button is selected "province' is my css value.

 $('.province').on('blur', 'input', sumtotal);

This works great and updates when the radio button loses focus. Is it possible to have the Tax Rate update on click.

 

I tried changing the line to:

$('.province').on('click', 'blur', 'input', sumtotal);

 

or 

$('.province').on('focus', 'blur', 'input', sumtotal);

but these Methods do not seem valid being used in the above line of code.

 

0 0

Replies

replied on June 19, 2015 Show version history

You can perform the calculation at both events but I think you should separate them:

  $ ('.province').on ('blur', 'input', SumTotal.);
  $ ('.province').on ('click', 'input', SumTotal.);

this does not affect the function and will run if click on the item or if the item loses focus

You can also try the change event. This is run when the element changes this is perfect to run a dynamic code.

  $ ('.province').on ('change', 'input', SumTotal.);

I hope this works for you

0 0
replied on June 19, 2015

Also, I think you're having a problem in the syntax and the whole class should this within '' should look like

$ (. 'Province') on ('change', 'input', SumTotal.);

replied on June 19, 2015

Separating them worked like a charm.

Thanks

0 0
replied on June 19, 2015

you're welcome

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

Sign in to reply to this post.