我使用switch
而不是if statement
构造,因为我有多个重复的语句,其中if statement
没错。
但是无法正确计算。我哪里错了? switch
结构无法正常工作。我使用了控制台,function sumUpNewCar
正常运行。
因此,我想执行框中的所有金额
function sprSelection() {
var sprCompleted = document.getElementById("sprCompleted");
var sprDoesntCompleted = document.getElementById("sprDoesntCompleted");
if(sprCompleted.checked) {
sprDoesntCompleted.disabled = true;
}
else if(sprDoesntCompleted.checked) {
sprCompleted.disabled = true;
}
else {
sprDoesntCompleted.disabled = false;
sprCompleted.disabled = false;
}
}
function prNewVehicleSelection (){
var twentyFiveCompleted = document.getElementById("twentyFiveCompleted");
var thirtyCompleted = document.getElementById("thirtyCompleted");
var thirtyFiveCompleted = document.getElementById("thirtyFiveCompleted");
if(twentyFiveCompleted.checked) {
thirtyCompleted.disabled = true;
thirtyFiveCompleted.disabled = true;
}
else if(thirtyCompleted.checked) {
twentyFiveCompleted.disabled = true;
thirtyFiveCompleted.disabled = true;
}
else if(thirtyFiveCompleted.checked) {
twentyFiveCompleted.disabled = true;
thirtyCompleted.disabled = true;
}
else {
twentyFiveCompleted.disabled = false;
thirtyCompleted.disabled = false;
thirtyFiveCompleted.disabled = false;
}
}
function sumUpNewCar(){
var promoLoan = Number (document.getElementById("promoLoan").innerHTML);
var standardLoan = Number (document.getElementById("newAuto2").innerHTML);
var promoPlusLoan = Number (document.getElementById("newAuto3").innerHTML);
var standardPlusLoan = Number (document.getElementById("newAuto4").innerHTML);
var twentyFiveCompleted = document.getElementById("twentyFiveCompleted");
var thirtyCompleted = document.getElementById("thirtyCompleted");
var thirtyFiveCompleted = document.getElementById("thirtyFiveCompleted");
var sprCompleted = document.getElementById("sprCompleted");
var sprDoesntCompleted = document.getElementById("sprDoesntCompleted");
var qualityCompleted = document.getElementById("qualityCompleted");
var qualityDoesntCompleted = document.getElementById("qualityDoesntCompleted");
var sumPromoLoansBonus = Number (document.getElementById("sumPromoLoansBonus").innerHTML);
var sumPromoPlusLoansBonus = Number (document.getElementById("sumPromoPlusLoansBonus").innerHTML);
var sumStandardLoansBonus = Number (document.getElementById("sumStandardLoansBonus").innerHTML);
var sumStandardPlusLoansBonus = Number (document.getElementById("sumStandardPlusLoansBonus").innerHTML);
var sumNewVehicle = Number (document.getElementById("sumNewVehicle").innerHTML);
switch(sumNewVehicle) {
case twentyFiveCompleted.checked:
sumPromoLoansBonus = Math.round (Number(promoLoan * 0.0155));
sumPromoPlusLoansBonus = Math.round (Number (promoPlusLoan * 0.0289));
sumStandardLoansBonus = Math.round (Number (standardLoan * 0.0321));
sumStandardPlusLoansBonus = Math.round (Number(standardPlusLoan * 0.0432));
break;
case thirtyCompleted.checked:
sumStandardLoansBonus = Math.round (Number (standardLoan * 0.0321));
sumStandardPlusLoansBonus = Math.round (Number(standardPlusLoan * 0.054321));
break;
case thirtyFiveCompleted.checked:
sumPromoLoansBonus= Math.round (Number(promoLoan * 0.01234));
sumPromoPlusLoansBonus = Math.round (Number (promoPlusLoan * 0.0321));
sumStandardLoansBonus= Math.round (Number (standardLoan * 0.066));
sumStandardPlusLoansBonus = Math.round (Number(standardPlusLoan * 0.7888));
break;
case sprCompleted.checked:
sumPromoLoansBonus = Math.round (Number(promoLoan * 0.01222));
sumPromoPlusLoansBonus = Math.round (Number (promoPlusLoan * 0.028989));
sumStandardLoansBonus = Math.round (Number (standardLoan * 0.02111));
sumStandardPlusLoansBonus = Math.round (Number(standardPlusLoan * 0.041111));
break;
case sprCompleted.checked && twentyFiveCompleted.checked:
sumStandardLoansBonus = Math.round (Number (standardLoan * 0.047868));
sumStandardPlusLoansBonus= Math.round (Number(standardPlusLoan * 0.056555));
break;
case sprCompleted.checked && thirtyFiveCompleted.checked:
sumPromoLoansBonus = Math.round (Number(promoLoan * 0.02222));
sumPromoPlusLoansBonus = Math.round (Number (promoPlusLoan * 0.0345));
sumStandardLoansBonus = Math.round (Number (standardLoan * 0.04445));
sumStandardPlusLoansBonus= Math.round (Number(standardPlusLoan * 0.0667);
break;
default:
sumPromoLoansBonus = Math.round (Number (promoLoan * 0.0015));
sumPromoPlusLoansBonus= Math.round (Number (promoPlusLoan * 0.0244));
sumStandardLoansBonus = Math.round (Number (standardLoan * 0.0244));
sumStandardPlusLoansBonus = Math.round (Number (standardPlusLoan * 0.03511));
break;
}
}
function calculate(){
var sumPromoLoansBonus = Number (document.getElementById("sumPromoLoansBonus").innerHTML);
var sumPromoPlusLoansBonus = Number (document.getElementById("sumPromoPlusLoansBonus").innerHTML);
var sumStandardLoansBonus = Number (document.getElementById("sumStandardLoansBonus").innerHTML);
var sumStandardPlusLoansBonus = Number (document.getElementById("sumStandardPlusLoansBonus").innerHTML);
var sumNewVehicle = document.getElementById("sumNewVehicle").innerHTML = Number (sumPromoLoansBonus + sumPromoPlusLoansBonus + sumStandardLoansBonus + sumStandardPlusLoansBonus );
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>revenue calculator Version 7</title>
</head>
<body>
<!--BOX #1-->
<div class="mainbox">
<form >
<input class="promo" type="number" min="1000" max="100000000" id="promoLoan" onchange="sumUpNewCar()">
<input class="standard" type="number" min="100000" max="100000000" id="newAuto2" onchange="sumUpNewCar()">
<input class="promoPlus" type="number" min="100000" max="100000000" id="newAuto3" onchange="sumUpNewCar()">
<input class="standardPlus" type="number" min="100000" max="100000000" id="newAuto4" onchange="sumUpNewCar()">
</form>
</div>
<!--BOX #2-->
<div class="mainbox2">
<label class="container4"><b>25%</b>
<input type="checkbox" class="input1" id="twentyFiveCompleted" onclick="prNewVehicleSelection(), sumUpNewCar(), calculate()">
</label>
<label class="container4"><b>30%</b>
<input type="checkbox" class="input1" id="thirtyCompleted" onclick="prNewVehicleSelection(), sumUpNewCar(), calculate()">
</label>
<label class="container4"><b>35%</b>
<input type="checkbox" class="input1" id="thirtyFiveCompleted" onclick="prNewVehicleSelection(), sumUpNewCar(), calculate()">
</label>
<label class="container" for="yes"><b> Да</b>
<input type="checkbox" class="input1" name="yes" id="sprCompleted" onclick="sprSelection(), sumUpNewCar()">
</label>
<label class="container" for="no"><b> Нет</b>
<input type="checkbox" class="input1" name="no" id="sprDoesntCompleted" onclick="sprSelection(), sumUpNewCar()">
</label>
<!--BOX#6-->
<div class="mainbox6">
<table class="tableOverall" >
<tr>
<td id="sumNewVehicle"></td>
</tr>
</table>
</div>
</div>
<table class="tableOverall2">
<tr>
<td id="sumPromoLoansBonus"></td> <!-- sum of promo box BASIC-->
<td id="sumPromoPlusLoansBonus"></td> <!-- sum of promoplus box BASIC-->
<td id="sumStandardLoansBonus"></td> <!-- sum of standard box BASIC-->
<td id="sumStandardPlusLoansBonus"></td> <!-- sum of standard plus box BASIC-->
</table>
答案 0 :(得分:0)
sumNewVehicle将具有哪些可能的值?它将返回您的情况下的一些数字。
二十个FiveCompleted.checked还将具有哪些值?我想它将给出True或False。
因此,如果这两个变量都具有不同的值,则您的切换大小写将永远无法工作。
其次,在不接受的情况下使用“ &&”条件。
即使在二十个案例中,“二十一万次完成。检查”将返回一个数字,这也是由于“ &&”条件而导致的。
例如:-您正在使用
case sprCompleted.checked && twentyFiveCompleted.checked:
在这种情况下,应使用if子句,而不要使用switch大小写。
只要您的条件返回正确的布尔值,它就可以在JavaScript中工作,但是与if语句相比,它没有其他优点。您的比较值是整数,但是大多数case表达式都可以解析为布尔值。主要是使用“ &&”条件的那些函数。
请参阅此链接以获取更多信息 javascript: using a condition in switch case
答案 1 :(得分:-1)
我认为您需要检查switch Case语法:
switch(expression) {
case n:
code block1
break;
case m:
code block 2
break;
default:
code block 3
}
等价于:
if (expression == n) {
code block 1
}
else if (expression == m) {
code block 2
}
else {
code block 3
}