該函數對我來說看起來很合適,但是當我運行它時,即使值爲"Extra Cheese"
,我在控制檯中返回的所有值也是0
。下面是我的代碼的一部分,所以runningTotal
和text1
在別處定義,但是這是一個不正常的部分:你檢查數組===
一個字符串,它會是否返回JavaScript函數總是返回0
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>";
};
'selectedCheese'看起來像一個'array'和你是比較數組字符串'「額外的奶酪」 /「沒有奶酪」 /「常規」'。這會工作嗎? – Panther
你想到哪個變量? – proti
正如Panther指出的那樣,您正在將一個數組的類型和值與一個字符串進行比較。因爲這個,你的言論都不會是真的。 –