-3

我对这段代码有疑问我对编程很陌生,不知道我做错了什么。当从 Web 浏览器运行该函数时,它说对象是预期的,但是在我向它添加一些代码之前它工作了 addveh 并删除

如果这是张贴此错误的地方,感到困惑抱歉

function ratech01(){
var numcars = document.getElementById('numars').value; 
var romcars = document.getElementById('removecars').value;
var addcars = document.getElementById('addcars').value;
var veh = 0;
var addveh = 0;
var removed = 0;
var Decision = '<span style="color:green">OK</span>';
var addloading = "(";
var busload = 0;
var inputload = +document.getElementById('load').value; 
for (var i = 0; i < numcars; i++){
     veh =  +veh + +document.getElementById('carval' + i).value;
}
for (var i = 0; i < romcars; i++){
     remove =  +remove + +document.getElementById('romcarval' + i).value;
}
for (var i = 0; i < addcars; i++){
     addveh =  +addveh + +document.getElementById('addcarval' + i).value;
}   
var addveh = addveh / 1000 * 5;
var remove = remove / 1000 * 5;
veh = veh / 1000 * 5;
vehend = veh + remove;

if (addveh != NaN || remove != NaN){
        if ( addveh != NaN && remove != NaN){
            addveh = veh + addveh - remove;
        }else if( addveh != NaN){
        addveh = veh - remove;
    }else{
    addveh = veh + addveh;
    }   

    }else {

    }  

if ( document.getElementById('club').value === "yes"){

    vehend = vehend + 41;
    addveh = addveh + 41;
    if (document.getElementById('inception').value === "no"){
    if ( vehend < 60 || addveh < 60){

        if ( vehend < 60 && addveh < 60){
            vehend = 60;
            addveh = 60;
        }else if(vehend < 60 ){
            vehend = 60;
        }else {
        addveh = 60;
        }
    }else{
    }

}else{
    if ( vehend < 76 || addveh < 76){

        if ( vehend < 76 && addveh < 76){
            vehend = 76;
            addveh = 76;
        }else if(vehend < 76 ){
            vehend = 76;
        }else {
        addveh = 76;
        }
    }else{
    } 
} 


}else {






    vehend = vehend + 67;
    addveh = addveh + 67;
    if (document.getElementById('inception').value === "no"){
    if ( vehend < 60 || addveh < 60){

        if ( vehend < 60 && addveh < 60){
            vehend = 60;
            addveh = 60;
        }else if(vehend < 60 ){
            vehend = 60;
        }else {
        addveh = 60;
        }
    }else{
    }

}else{
    if ( vehend < 102 || addveh < 102){

        if ( vehend < 102 && addveh < 102){
            vehend = 102;
            addveh = 102;
        }else if(vehend < 102 ){
            vehend = 102;
        }else {
        addveh = 102;
        }
    }else{
    } 
}



if ( document.getElementById('garage').value === "no"){
    if ( document.getElementById('area').value == 1){

    }else if ( document.getElementById('area').value == 2){
        var Decision = "Please refer";  
    }else if ( document.getElementById('area').value == 3){
        var Decision = '<span style="color:red">Decline car must be garaged</span>';
    }else{
        var Decision = '<span style="color:red">Decline car must be garaged</span>';
    }

}   
if ( document.getElementById('lience').value == 1){
    var Decision = '<span style="color:red">Decline Driver must have licence more than 1 year</span>';

}else if ( document.getElementById('lience').value == 2){
    var addloading = addloading + '15% loading for 1-2 years driving <br/>';
    var year1 = vehend / 100 * 15;
    vehend = vehend + year1;
    var year2 = addveh / 100 * 15;
    addveh = addveh + year2;
}

if (inputload != ""){
    var addloading = addloading + " " + inputload + '% for user input) <br/>';
    var conload = vehend / 100 * inputload;
    vehend = vehend + conload;
    var conload2 = addveh / 100 * inputload;
    addveh = addveh + conload2;
}else {
    var addloading = addloading + ")";
}

if ( document.getElementById('business').value === "yes"){

    var addloading = addloading + " " + '30% business use <br/>';
    var businload = veh / 100 * 30;
    var businload2 = addveh / 100 * 30;
    vehend = vehend + businload;
    addveh = addveh + businload2;
}

    vehandipt = (vehend / 100 * 6) + vehend;
    addandipt = (addveh / 100 * 6) + addveh;

document.getElementById('result').innerHTML = vehend + " " + Decision + " " + addloading + " " + vehandipt;


}
4

2 回答 2

1

下次您通过 js 代码时,请使用jsbeautifier之类的格式对其进行格式化,这是我的两分钱:

var addveh - 重复声明

var 已删除- 应该删除?// Nabeel Sheikh 看到了

var Decision - 重复声明

var addloading - 重复声明

vehend - 未声明的全局变量?

vehandipt - 未声明的全局变量?

addandipt - 未声明的全局变量?

} - 未闭合的花括号

可疑: veh = +veh + + document.getElementById('romcarval' + i).value;

尝试这个:

function ratech01() {
    var numcars = document.getElementById('numars').value;
    var romcars = document.getElementById('removecars').value;
    var addcars = document.getElementById('addcars').value;
    var veh = 0;
    var addveh = 0;
    var remove = 0;
    var Decision = '<span style="color:green">OK</span>';
    var addloading = "(";
    var busload = 0;
    var inputload = +document.getElementById('load').value;
    for (var i = 0; i < numcars; i++) {
        veh = +veh + +document.getElementById('carval' + i).value;
    }
    for (var i = 0; i < romcars; i++) {
        remove = +remove + +document.getElementById('romcarval' + i).value;
    }
    for (var i = 0; i < addcars; i++) {
        addveh = +addveh + +document.getElementById('addcarval' + i).value;
    }
    addveh = addveh / 1000 * 5;
    remove = remove / 1000 * 5;
    veh = veh / 1000 * 5;
    var vehend = veh + remove;

    if (addveh != NaN || remove != NaN) {
        if (addveh != NaN && remove != NaN) {
            addveh = veh + addveh - remove;
        } else if (addveh != NaN) {
            addveh = veh - remove;
        } else {
            addveh = veh + addveh;
        }

    } else {

    }

    if (document.getElementById('club').value === "yes") {

        vehend = vehend + 41;
        addveh = addveh + 41;
        if (document.getElementById('inception').value === "no") {
            if (vehend < 60 || addveh < 60) {

                if (vehend < 60 && addveh < 60) {
                    vehend = 60;
                    addveh = 60;
                } else if (vehend < 60) {
                    vehend = 60;
                } else {
                    addveh = 60;
                }
            } else {}

        } else {
            if (vehend < 76 || addveh < 76) {

                if (vehend < 76 && addveh < 76) {
                    vehend = 76;
                    addveh = 76;
                } else if (vehend < 76) {
                    vehend = 76;
                } else {
                    addveh = 76;
                }
            } else {}
        }


    } else {


        vehend = vehend + 67;
        addveh = addveh + 67;
        if (document.getElementById('inception').value === "no") {
            if (vehend < 60 || addveh < 60) {

                if (vehend < 60 && addveh < 60) {
                    vehend = 60;
                    addveh = 60;
                } else if (vehend < 60) {
                    vehend = 60;
                } else {
                    addveh = 60;
                }
            } else {}

        } else {
            if (vehend < 102 || addveh < 102) {

                if (vehend < 102 && addveh < 102) {
                    vehend = 102;
                    addveh = 102;
                } else if (vehend < 102) {
                    vehend = 102;
                } else {
                    addveh = 102;
                }
            } else {}
        }


        if (document.getElementById('garage').value === "no") {
            if (document.getElementById('area').value == 1) {

            } else if (document.getElementById('area').value == 2) {
                Decision = "Please refer";
            } else if (document.getElementById('area').value == 3) {
                Decision = '<span style="color:red">Decline car must be garaged</span>';
            } else {
                Decision = '<span style="color:red">Decline car must be garaged</span>';
            }

        }
        if (document.getElementById('lience').value == 1) {
            Decision = '<span style="color:red">Decline Driver must have licence more than 1 year</span>';

        } else if (document.getElementById('lience').value == 2) {
            addloading = addloading + '15% loading for 1-2 years driving <br/>';
            var year1 = vehend / 100 * 15;
            vehend = vehend + year1;
            var year2 = addveh / 100 * 15;
            addveh = addveh + year2;
        }

        if (inputload != "") {
            addloading = addloading + " " + inputload + '% for user input) <br/>';
            var conload = vehend / 100 * inputload;
            vehend = vehend + conload;
            var conload2 = addveh / 100 * inputload;
            addveh = addveh + conload2;
        } else {
            addloading = addloading + ")";
        }

        if (document.getElementById('business').value === "yes") {

            addloading = addloading + " " + '30% business use <br/>';
            var businload = veh / 100 * 30;
            var businload2 = addveh / 100 * 30;
            vehend = vehend + businload;
            addveh = addveh + businload2;
        }

        var vehandipt = (vehend / 100 * 6) + vehend;
        var addandipt = (addveh / 100 * 6) + addveh;

        document.getElementById('result').innerHTML = vehend + " " + Decision + " " + addloading + " " + vehandipt;


    }
}
于 2013-10-08T10:02:45.630 回答
0

你有几个问题:

  1. 如前所述,您声明(使用 var)addveh 两次。

  2. 您为 addveh 分配值 0 并且您的下一个操作是:

    addveh = addveh / 1000 * 5;

在这一点上(一旦你删除了第二个错误的var)addveh 将始终为零,因此结果将始终为零,我想这不是你需要的吗?

于 2013-10-08T09:43:02.573 回答