我有一个类似的Switch语句。

switch (rsCash.length){
case 1:
    document.getElementById("cash3").innerHTML = rsCash + "Gp";
    break;
case 2:
    document.getElementById("cash3").innerHTML = rsCash + "Gp";
    break;
case 3:
    document.getElementById("cash3").innerHTML = rsCash + "Gp";
    break;
case 4:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut1) + "k";
    break;
case 5:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut2) + "k";
    break;
case 6:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut3) + "k";
    break;
case 7:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut1) + "M";
    break;
case 8:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut2) + "M";
    break;
case 9:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut3) + "M";
    break;
case 10:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut4) + "M";
    break;
case 11:
    document.getElementById("cash3").innerHTML = rsCash.substring(0,cut5) + "M";
    break;


}

并且当rsCash变量Changes时,它不会更新。

HTML是。

<h1 id="RsMoney">RsCash:<b id="cash3">0 Gp</b></h1>


我尝试将switch语句放入每秒调用一次的函数中,但仍然无法正常工作

我已将整个代码复制并粘贴到JavaScript皮棉中,未发现任何错误,Google Chrome也未发现任何错误

变量在此通过此功能更改。

function OpenCrate(){
if(Crates != 0){
var RewardNumber = Math.floor((Math.random() * 15)+ 1);
switch (RewardNumber){
case 1:
    rsCash = rsCash + 200000;
    document.getElementById("Log").innerHTML = "$200k was found!";
    break;
case 2:
    rsCash = rsCash + 25000;
    document.getElementById("Log").innerHTML = "$15 was found!";
    break;
case 3:
    rsCash = rsCash + 5000;
    document.getElementById("Log").innerHTML = "5k was found!";
    break;
case 4:
    rsCash = rsCash + 500000;
    document.getElementById("Log").innerHTML = "500k was found!";
    break;
case 5:
    rsCash = rsCash + 75000;
    document.getElementById("Log").innerHTML = "75k was found!";
    break;
case 6:
    rsCash = rsCash + 5000000;
    document.getElementById("Log").innerHTML = "5m was found!";
    break;
case 7:
    rsCash = rsCash + 275000;
    document.getElementById("Log").innerHTML = "275k was found!";
    break;
case 8:
    rsCash = rsCash + 5000;
    document.getElementById("Log").innerHTML = "5k was found!";
    break;
case 9:
    rsCash = rsCash + 15000;
    document.getElementById("Log").innerHTML = "15k was found!";
    break;
case 10:
    rsCash = rsCash + 8000;
    document.getElementById("Log").innerHTML = "8k was found!";
    break;
case 11:
    rsCash = rsCash + 35000;
    document.getElementById("Log").innerHTML = "35k was found!";
    break;
case 12:
    rsCash = rsCash + 125000;
    document.getElementById("Log").innerHTML = "125k was found!";
    break;
case 13:
    rsCash = rsCash + 80000;
    document.getElementById("Log").innerHTML = "80k was found!";
    break;
case 14:
    rsCash = rsCash + 600000;
    document.getElementById("Log").innerHTML = "600k was found!";
    break;
case 15:
    rsCash = rsCash + 12000;
    document.getElementById("Log").innerHTML = "12k was found!";
    break;
}

最佳答案

rsCash是一个数字:

rsCash = rsCash + 200000;


但您尝试使用它的长度:

switch (rsCash.length) ...


您无法做到这一点-获得undefined,因此您的case都不会被解雇。您需要先将rsCash转换为字符串,然后再取其长度。像这样的东西:

switch (rsCash.toString().length) ...

09-25 19:23