jquery calculate values of two select boxes

159 views Asked by At

I am trying to calculate the value of a shopping card based on selected quantity and selected transportation. My code can be tested here http://jsfiddle.net/CsPL4/ My jquery is

$(document).ready(function() {


    $("#transportation option","#quantity option").filter(function() {
        return $(this).val() == $("#total").val();
    }).attr('selected', true);

    $("#transportation,#quantity").live("change", function() {
        var total = Number(24.9 * $('#quantity').val());
        var transport = Number($(this).find("option:selected").attr("value"));
        var total_final = total + transport;
        total = total.toFixed(2); 
        total_final = total_final.toFixed(2); 
        if (total > 150 && transport == 9.9){ 
            $("#total").val(total);
        }else if (total <= 150 && transport == 9.9){
            $("#total").val(total_final);
        }else if (total > 250 && transport == 18.9){
            $("#total").val(total);
        }else if (total <= 250 && transport == 18.9){
            $("#total").val(total_final);
        }
    });

});

The problem is that for example i select to buy 2 pieces and select first option for transportation and than i change only the quantity to 1 piece for example, it will keep showing the old value of total charge - for 2 pieces selected.

I can't figure out what I'm missing.

Thank you, Cata

1

There are 1 answers

0
Pedro Moreira On

This code may help you

jQuery('#quantity, #transportation').change(function() {
   var quantity = parseFloat(jQuery('#quantity').val());
   var transportation = parseFloat(jQuery('#transportation').val());
   jQuery('#total').val(transportation * quantity);
});