2017-10-04 137 views
1

該函數對我來說看起來很合適,但是當我運行它時,即使值爲"Extra Cheese",我在控制檯中返回的所有值也是0。下面是我的代碼的一部分,所以runningTotaltext1在別處定義,但是這是一個不正常的部分:你檢查數組===一個字符串,它會是否返回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>"; 
}; 
+1

'selectedCheese'看起來像一個'array'和你是比較數組字符串'「額外的奶酪」 /「沒有奶酪」 /「常規」'。這會工作嗎? – Panther

+0

你想到哪個變量? – proti

+0

正如Panther指出的那樣,您正在將一個數組的類型和值與一個字符串進行比較。因爲這個,你的言論都不會是真的。 –

回答

0

這就是我想出了和它的工作,謝謝大家的幫助和指點。

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); 
    }; 
0

不管你做什麼都是假的。既然你聲明cheseTotal = 0在最頂端,它會檢查你所有的條件,在所有條件中都返回false,並且什麼都不做。所以當你console.log(cheeseTotal)時,它會返回你在最上面聲明的值。

你想要的是看是否數組includes的字符串:

if (selectedCheese.includes("Extra Cheese")) { 
     cheeseTotal = 3; 
    } else if... 

下面是關於方法的一些信息:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

+0

或更多跨瀏覽器兼容性: 'if(selectedCheese.indexOf(「ExtraChaese」)){{0} {0} {0} {0}} cheeseTotal = 3; } else if ...' – proti

+0

好奇,這是否真的有用?如果「額外奶酪」不存在,它會返回'-1',這似乎會返回true:https://codepen.io/foozie3moons/pen/OxOBXj – Matthew

+0

會有這樣的工作嗎? –

0

,我發現了一些東西你的代碼錯誤。對於初學者來說,你的if子句也應該在for循環中。您正在使用不需要的第二個數組(selectedCheese)。其次,您可以通過使用+=運算符來添加總計,這使得runningtotal也不需要。我不確定該函數的參數是什麼(或爲什麼需要)我建議重新看一下,因爲它看起來像你可以刪除它們。

下面是針對您的問題的解決方案的運行片段。

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) { 
 
     var cheeseVal = cheeseArray[m].value; 
 
     console.log("selected cheese item: (" + cheeseVal + ")"); 
 
     if (cheeseVal === "Extra Cheese") { 
 
     cheeseTotal += 3; 
 
     } else if (cheeseVal === "No Cheese") { 
 
     cheeseTotal += 0; 
 
     } else if (cheeseVal === "Regular") { 
 
     cheeseTotal += 0; 
 
     } 
 
     text1 = text1 + "<br>" + cheeseVal ; 
 
    } 
 
    } 
 
    console.log(cheeseTotal); 
 
    document.getElementById("showText").innerHTML = text1; 
 
    document.getElementById("totalPrice").innerHTML = "</h3>Total: <strong>$" + cheeseTotal + ".00" + "</strong></h3>"; 
 
};
<input type="checkbox" class="cheese" value="Extra Cheese">Extra Cheese</input> 
 
<input type="checkbox" class="cheese" value="No Cheese">No Cheese</input> 
 
<input type="checkbox" class="cheese" value="Regular">Regular</input> 
 
<button onclick="getCheese(0, 'Cheeses')"> submit </button> 
 

 
<div id="showText"></div> 
 
<div id="totalPrice"></div>

+0

運行總數是爲了跟蹤比薩的總價,絕對是我沒有解釋整個腳本功能的錯。該網站是比薩訂購網站,這是你可以添加到披薩的許多東西之一。 –

+0

'cheeseTotal' var現在是你的'runningtotal'。上面的例子對你有意義嗎?讓我知道它是否沒有意義 –