JavaScript函数始终返回0

时间:2017-10-04 17:11:57

标签: javascript html5

这个功能对我来说是正确的,但是当我运行它时,即使值为0,我在控制台中取回的所有内容都是"Extra Cheese"。以下是我的代码的一部分,因此runningTotaltext1在其他地方定义,但这是不起作用的部分:

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>";
};

3 个答案:

答案 0 :(得分:0)

你正在检查一个数组===是否为一个字符串,无论你做什么,它都会返回false。由于您在最顶层声明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

答案 1 :(得分:0)

我发现你的代码有些问题。对于初学者来说,你的if子句也应该在for循环中。您正在使用不需要的第二个数组(selectedCheese)。其次,您可以使用+=运算符添加总计,这也使您不需要runningtotal。我不确定该函数的参数是什么(或者为什么需要它们)我建议重新看一下,因为它似乎可以删除它们。

以下是针对您的问题的解决方案的运行摘要。

&#13;
&#13;
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>";
};
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分: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);
    };