JavaScript函数始终返回0
这个函数在我看来是正确的,但是当我运行它时,我得到的只是控制台中的JavaScript函数始终返回0,javascript,html,Javascript,Html,这个函数在我看来是正确的,但是当我运行它时,我得到的只是控制台中的0,即使值是“Extra Cheese”。下面是我代码的一部分,因此在别处定义了runningTotal和text1,但这部分不起作用: function getCheese(runningTotal,text1) { var cheeseTotal = 0; var selectedCheese = []; var cheeseArray = document.getElementsByClassName
0
,即使值是“Extra Cheese”
。下面是我代码的一部分,因此在别处定义了runningTotal
和text1
,但这部分不起作用:
function getCheese(runningTotal,text1) {
var cheeseTotal = 0;
var selectedCheese = [];
var cheeseArray = document.getElementsByClassName("cheese");
for (var m = 0; m < cheeseArray.length; m++) {
if (cheeseArray[m].checked) {
selectedCheese.push(cheeseArray[m].value);
console.log("selected cheese item: ("+cheeseArray[m].value+")");
text1 = text1+cheeseArray[m].value+"<br>";
}
}
if (selectedCheese === "Extra Cheese") {
cheeseTotal = 3;
}
else if (selectedCheese === "No Cheese") {
cheeseTotal = 0;
}
else if (selectedCheese === "Regular") {
selectedCheese = 0;
}
runningTotal = (runningTotal + cheeseTotal);
console.log(cheeseTotal);
document.getElementById("showText").innerHTML=text1;
document.getElementById("totalPrice").innerHTML = "</h3>Total: <strong>$"+runningTotal+".00"+"</strong></h3>";
};
函数getCheese(runningTotal,text1){
var cheeseTotal=0;
var selectedCheese=[];
var cheeseArray=document.getElementsByClassName(“cheese”);
对于(var m=0;m您正在检查数组是否是字符串,无论您做什么,该字符串都将返回false。由于您在最顶端声明了
cheseTotal=0
,它将检查所有条件,对所有条件返回false,而不执行任何操作。因此,当您console.log(cheeseTotal)
时,它将返回您在顶部声明的值
您需要查看数组是否包含字符串:
if (selectedCheese.includes("Extra Cheese")) {
cheeseTotal = 3;
} else if...
以下是有关该方法的更多信息:
您正在检查数组是否是字符串,无论您做什么,该字符串都将返回false。由于您在最顶端声明了cheseTotal=0
,它将检查所有条件,对所有条件返回false,而不执行任何操作。因此,当您console.log(cheeseTotal)
时,它将返回您在顶部声明的值
您需要查看数组是否包含字符串:
if (selectedCheese.includes("Extra Cheese")) {
cheeseTotal = 3;
} else if...
以下是有关该方法的更多信息:
我发现您的代码有一些地方出错。首先,if子句也应该在for循环中。您正在使用不需要的第二个数组(selectedCheese
)。第二,您可以使用+=
操作符添加总计,这使得您的runningtotal
也不需要。我不确定函数的参数是什么(或者为什么需要这些参数),我建议您重新考虑一下,因为您似乎可以删除它们
下面是我针对您的问题的解决方案的运行片段
函数getCheese(runningTotal,text1){
var cheeseTotal=0;
var selectedCheese=[];
var cheeseArray=document.getElementsByClassName(“cheese”);
对于(var m=0;m”+cheeseVal;
}
}
console.log(cheeseTotal);
document.getElementById(“showText”).innerHTML=text1;
document.getElementById(“totalPrice”).innerHTML=“总计:$”+cheeseTotal+”.00“+””;
};代码>
额外的奶酪
没有奶酪
有规律的
提交
我发现你的代码有一些地方不对劲。首先,if子句也应该在for循环中。您正在使用不需要的第二个数组(selectedCheese
)。第二,您可以使用+=
操作符添加总计,这使得您的runningtotal
也不需要。我不确定函数的参数是什么(或者为什么需要这些参数),我建议您重新考虑一下,因为您似乎可以删除它们
下面是我针对您的问题的解决方案的运行片段
函数getCheese(runningTotal,text1){
var cheeseTotal=0;
var selectedCheese=[];
var cheeseArray=document.getElementsByClassName(“cheese”);
对于(var m=0;m”+cheeseVal;
}
}
console.log(cheeseTotal);
document.getElementById(“showText”).innerHTML=text1;
document.getElementById(“totalPrice”).innerHTML=“总计:$”+cheeseTotal+”.00“+””;
};代码>
额外的奶酪
没有奶酪
有规律的
提交
这是我想到的,而且很有效,谢谢大家的帮助和指点
function getCheese(runningTotal,text1) {
var cheeseTotal = 0;
var cheeseArray = document.getElementsByClassName("cheese");
for (var m = 0; m < cheeseArray.length; m++) {
if (cheeseArray[m].checked) {
var selectedCheese=(cheeseArray[m].value);
text1 = text1+selectedCheese+"<br>";
}
}
if (selectedCheese === "Extra Cheese") {
runningTotal = runningTotal + 3;
}
else
{ cheeseTotal = 0;
}
runningTotal = (runningTotal + cheeseTotal);
document.getElementById("showText").innerHTML=text1;
document.getElementById("totalPrice").innerHTML = "</h3>Total: <strong>$"+runningTotal+".00"+"</strong></h3>";
getCrust(runningTotal,text1);
};
函数getCheese(runningTotal,text1){
var cheeseTotal=0;
var cheeseArray=document.getElementsByClassName(“cheese”);
对于(var m=0;m”;
}
}
如果(selectedCheese==“额外的奶酪”){
runningTotal=runningTotal+3;
}
其他的
{cheeseTotal=0;
}
runningTotal=(runningTotal+cheeseTotal);
document.getElementById(“showText”).innerHTML=text1;
document.getElementById(“totalPrice”).innerHTML=“总计:$”+runningTotal+”.00”+