JavaScript, область действия функции - глобальная переменная NaN - PullRequest
0 голосов
/ 14 марта 2012

Мне тяжело с этими тремя интерактивными функциями.Кулак собирает значения из формы и использует ее с некоторыми переменными, установленными вне функции

    /* -------------------- Varibales set for use through script --------- */

        var price_set = <?= json_encode($price_set); ?>;
        var setup = <?= json_encode($setup); ?>;
        var total = 0;
        var qty = <?php echo $qty; ?>;
        var total_gprice = <?php echo $total_g_price; ?>;

        var gprice = total_gprice/qty;

        var setup_total = 0; 
    /* ---------------------------------------*/

    function price_func(){
        total = 0;
        setup_total = 0; // if the page is re-freshed everything is recalculated

        $('.colors').each(function(){
           color_num = $(this).val();

           if (color_num != 0){

               //set price each based on json array
               pe = parseFloat(price_set[color_num]); 

            }

               else {
                    pe=0;
               }

           pe = pe.toFixed(2);

           //set price each based on json array
           setup_each = parseFloat(setup[color_num]); 

           pe_total(pe);
           setup_calc(setup_each);
         });


        alert(setup_total); // alert NaN???? <------------------- Problem

        total = total + parseFloat(gprice);
        total = total.toFixed(2);
        $('#price_each').html('$'+total); <-- total variables work hence confusion
    };

    function pe_total(add){
        total = parseFloat(total) + parseFloat(add);
        }

    function setup_calc(setup_each){
        setup_each = setup_each/qty;
        setup_total += setup_each;
        /* if i alert(setup_total) here it will calculate each addition correctly
    }

HTML-вывод

var price_set = {"1":"2.50","2":"3.50","3":"4.50","4":"5.50","5":"6.50","6":"7.50"};
var setup = {"1":"20.00","2":"40.00","3":"60.00","4":"80.00","5":"100.00","6":"120.00"};
var total = 0;
var qty = 6;
var total_gprice = 42;

var gprice = total_gprice/qty;

var setup_total = 0;
$("input:checkbox").click(function(){
    x=($(this).attr('id'));
     x = x+'1';
    $('.'+x).toggle();

});

$('.colors').change(function(){
    price_func();
});

function price_func(){
    total = 0;
    setup_total = 0;
    $('.colors').each(function(){
       color_num = $(this).val();
       if (color_num != 0){
        pe = parseFloat(price_set[color_num]);
       }
        else {
            pe=0;
        }
       pe = pe.toFixed(2);
       setup_each = parseFloat(setup[color_num]);
       pe_total(pe);
       setup_calc(setup_each);
     });


alert(parseFloat(setup_total));
    total = total + parseFloat(gprice);
    total = total.toFixed(2);
    $('#price_each').html('$'+total);
};

function pe_total(add){
    total = parseFloat(total) + parseFloat(add);
    }

function setup_calc(setup_each){
    setup_each = setup_each/qty;
    setup_total += setup_each;
}

Я думаю, что я не понимаю интерпретацию сценариев Java строки против числаили сфера или оба .. ugg.

Когда я делаю это изменение, все работает ... НО ПОЧЕМУ?

function price_func(){
    total = 0;
    setup_total = 0;
    $('.colors').each(function(){
       color_num = $(this).val();
       if (color_num != 0){
        pe = parseFloat(price_set[color_num]);
        setup_each = parseFloat(setup[color_num]); // PLACED THIS VARIABLE HERE WITH parseFloat() - - -- ?
       }
        else {
            pe=0;
            setup_each = 0; // added this - - - 
        }
       pe = pe.toFixed(2);
       setup_each = setup_each.toFixed(2); // ADDED THIS


       pe_total(pe);
       setup_calc(setup_each);
     });
...