Peter
Peter

Reputation: 135

jquery value calculation depending on drop down selection

This my code:

$('#paying_by').click(function()
{       
       if ($(this).val() == "GBPTRF")
       {
          $('.currency').html('&#163 ');
          $('#c_card_rate').val("0.00");
          $('#c_card_amount').val("0.00");
          $('.total_inc_charges').val(); /*???? refresh? this is the question*/
          $('.c_card_field').hide();            
       }                

       if ($(this).val() == "GBPCCARD")
       {
          $('.currency').html('&#163 ');
          $('.c_card_field').show();    
       }    

});

By default the c_card_field is hidden. If the user clicks on GBPCCARD then 2 fields appear within the c_card_field. One of them is the c_card_rate which is a %. Then the total amount increses with amount_due*c_card_rate/100. If the user selects GBPTRF again then, the c_card_field disappears. Of course I clear the the c_card_rate field but the total amount which has been increased by the c_card_amount(amount_due*c_card_rate/100) remains increased until I amend the amoun_due field.

Question: How can I refresh the total_inc_charges when the user clicks GBPTRF again?

Thank you.

Upvotes: 0

Views: 600

Answers (1)

lalibi
lalibi

Reputation: 3078

Based on your last comment. I would refactor your code like this:

var calc_total_inc_charges = function () {
    var sum1 = parseFloat($('input[name=sum1]').val()) || 0;
    var sum2 = parseFloat($('input[name=sum2]').val()) || 0;
    var sum3 = parseFloat($('input[name=sum3]').val()) || 0;
    return (sum1*sum3/100)+sum1;
};

$(function() {
    $('input[name^=sum]').keyup(function() {
        $('#total_inc_charges').val(calc_total_inc_charges());
    });
});

Now you can call calc_total_inc_charges() whenever you like to recalculate charges.

Upvotes: 1

Related Questions