功能正在运行一半,但只能在线

时间:2013-11-01 22:59:44

标签: javascript

我遇到了问题,我有一个jquery的巨大功能。当我创建它,经过一些设置,工作正常。当我离线使用它时,它可以工作,但是当我在网站上传页面时,会发生一些奇怪的事情:

这项功能只有一半!听起来很有趣,但我不明白为什么会这样做。

公式为:

function calcolo_peso() {
    var forza = $('#for').val();
    var clegg = 0;
    var cmed = 0;
    var cpes = 0;
    var taglia = 0;
    if (forza == 0) {
        var clegg = 0;
        var cmed = 0.1;
        var cpes = 0.2
    } else if (forza == 1) {
        var clegg = 1.5;
        var cmed = 3;
        var cpes = 5
    } else if (forza == 2) {
        var clegg = 3;
        var cmed = 6.5;
        var cpes = 10
    } else if (forza == 3) {
        var clegg = 5;
        var cmed = 10;
        var cpes = 15
    } else if (forza == 4) {
        var clegg = 6.5;
        var cmed = 13;
        var cpes = 20
    } else if (forza == 5) {
        var clegg = 8;
        var cmed = 16.5;
        var cpes = 25
    } else if (forza == 6) {
        var clegg = 10;
        var cmed = 20;
        var cpes = 30
    } else if (forza == 7) {
        var clegg = 11.5;
        var cmed = 23;
        var cpes = 35
    } else if (forza == 8) {
        var clegg = 13;
        var cmed = 26.5;
        var cpes = 40
    } else if (forza == 9) {
        var clegg = 15;
        var cmed = 30;
        var cpes = 45
    } else if (forza == 10) {
        var clegg = 16.5;
        var cmed = 33;
        var cpes = 50
    } else if (forza == 11) {
        var clegg = 19;
        var cmed = 38;
        var cpes = 57.5
    } else if (forza == 12) {
        var clegg = 21.5;
        var cmed = 43;
        var cpes = 65
    } else if (forza == 13) {
        var clegg = 25;
        var cmed = 50;
        var cpes = 75
    } else if (forza == 14) {
        var clegg = 29;
        var cmed = 58;
        var cpes = 87.5
    } else if (forza == 15) {
        var clegg = 33;
        var cmed = 66.5;
        var cpes = 100
    } else if (forza == 16) {
        var clegg = 38;
        var cmed = 76.5;
        var cpes = 115
    } else if (forza == 17) {
        var clegg = 43;
        var cmed = 86.5;
        var cpes = 130
    } else if (forza == 18) {
        var clegg = 50;
        var cmed = 100;
        var cpes = 150
    } else if (forza == 19) {
        var clegg = 58;
        var cmed = 116;
        var cpes = 175
    } else if (forza == 20) {
        var clegg = 66.5;
        var cmed = 133;
        var cpes = 200
    } else if (forza == 21) {
        var clegg = 76.5;
        var cmed = 153;
        var cpes = 230
    } else if (forza == 22) {
        var clegg = 86.5;
        var cmed = 173;
        var cpes = 260
    } else if (forza == 23) {
        var clegg = 100;
        var cmed = 200;
        var cpes = 300
    } else if (forza == 24) {
        var clegg = 116.5;
        var cmed = 233;
        var cpes = 350
    } else if (forza == 25) {
        var clegg = 133;
        var cmed = 266.5;
        var cpes = 400
    } else if (forza == 26) {
        var clegg = 153;
        var cmed = 306.5;
        var cpes = 460
    } else if (forza == 27) {
        var clegg = 173;
        var cmed = 346.5;
        var cpes = 520
    } else if (forza == 28) {
        var clegg = 200;
        var cmed = 400;
        var cpes = 600
    } else if (forza == 29) {
        var clegg = 233;
        var cmed = 466.5;
        var cpes = 700
    } else if (forza == 30) {
        var clegg = 266;
        var cmed = 532;
        var cpes = 800
    } else {
        var clegg = 299;
        var cmed = 306;
        var cpes = 900
    }
    if ($('#tagliapersonaggio').val() == "Piccola") {
        var taglia = 0.75
    }
    if ($('#tagliapersonaggio').val() == "Media") {
        var taglia = 1
    }
    if ($('#tagliapersonaggio').val() == "Grande") {
        var taglia = 2
    }
    var terreno = cpes * 2;
    var trascinare = cpes * 5;
    var caricoleggero = clegg * taglia;
    var caricomedio = cmed * taglia;
    var caricopesante = cpes * taglia;
    $('#car_legg').val(caricoleggero);
    $('#car_med').val(caricomedio);
    $('#car_pes1, #car_pes2').val(caricopesante);
    $('#dalterreno').val(terreno);
    $('#trascinare').val(trascinare);
}

输出必须 6输入填充最终值但仅在线其中2个结果已完全填充,最后两个 ..

任何猜测??

1 个答案:

答案 0 :(得分:1)

对每个变量使用var就足够了。:

function calcolo_peso(){
    var forza = $('#for').val();
    var clegg = 0;
    var cmed = 0;
    var cpes = 0;
    var taglia = 0;

    if (forza == 0) {
        clegg = 0;
        cmed = 0.1;
        cpes = 0.2
    } else if (forza == 1) {
        clegg = 1.5;
        cmed = 3;
        cpes = 5
    } ...

    if ($('#tagliapersonaggio').val() == "Piccola") {
        taglia = 0.75
    } ...

    terreno = cpes * 2;
    trascinare = cpes * 5;
    caricoleggero = clegg * taglia;
    ....

最后两个字段不受taglia值的影响。我认为您必须确保taglia具有正确的数值。