Javascript开关盒不支持';即使情况属实,也不能工作

Javascript开关盒不支持';即使情况属实,也不能工作,javascript,Javascript,由于我之前的问题无法得到任何解决问题的答案,我决定发布我的全部代码来确定问题。问题是当按下按钮时,即使三个图像中的一个相同(等于所有其他图像),例如(出现三个1或三个2),也不会输出我在程序中输入的消息,不会发生任何事情 这是我原来的问题 HTML: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> &

由于我之前的问题无法得到任何解决问题的答案,我决定发布我的全部代码来确定问题。问题是当按下按钮时,即使三个图像中的一个相同(等于所有其他图像),例如(出现三个1或三个2),也不会输出我在程序中输入的消息,不会发生任何事情

这是我原来的问题

HTML:

<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
    </head>

    <body>
        <form name=slots onSubmit="rollem(); return false;">
            <tr>
                <th align=right>UserTokens:</th>
                <td align=left>
                    <input type=box size=5 name=UserTokens READONLY value=25>
                </td>
            </tr>
            <tr>
                <th align=right>Your bet:</th>
                <td align=left>
                    <input type=box size=5 name=bet>
                </td>
            </tr>
            <tr>
                <th>
                    <input type=submit value="Spin the slots">
                </th>
                <center>
                    <table cellspacing=5 cellpadding=2 border=0>
                        <tr>
                            <td>
                                <img src=number1test.gif name=slot1>
                            </td>
                            <td>
                                <img src=number2test.gif name=slot2>
                            </td>
                            <td>
                                <img src=number3test.gif name=slot3>
                            </td>
                        </tr>
                    </table>
                    <input type=text readonly size=33 name=banner>
                    </td>
            </tr>
        </form>
    </body>

</html>

</html>

无标题文件
用户令牌:
您的赌注:
和我的JS(在
末尾):

slotitem=new数组('number1test','number2test','number3test');//为每个插槽项目映像创建数组
令牌=100;//起始代币
函数stopplay(){//调用函数
if(document.slots.UserTokens.value1){
document.slots.banner.value=“Bet is”+document.slots.Bet.value+“UserTokens碎片”;
}否则{
document.slots.banner.value=“Bet is”+document.slots.Bet.value+“UserTokens块”;
}
计数器=0;
棘突();
}
函数spinem(){//随机生成图片的速度
转数1=10+数学楼层((数学随机()*5))
对于(a=0;a
添加

开关()之前,您将看到正在检查的实际值。您缺少图像的扩展名(
.gif
)和URL的第一部分(
http://.../


我认为当您更改图像时,您应该记住插槽的值(在某些变量中),而不是从图像的
src
属性中读取它们。

我看到您在哪里设置document.slots.slot1.src=“number1test.gif”等等,但在哪里都没有设置document.slots.slot=“number1test”

我的猜测是,你使用了错误的东西,加上使用了错误的值


好的,你想要一个更完整的答案

第一,你的设计是错误的。您永远不应该使用像图像源文本这样变化无常的东西来确定事物是否等价。在进行随机化时进行等效性测试,或者至少存储随机化结果以备日后交叉检查。将你的mod(%)从源图像集中移出,得到你的最终数字,检查这些数字,然后设置你的“你赢了”和结果中的图像

第二,您的错误设计是错误的,因为您没有使用最初设置的变量(您正在打开对象),然后您正在与未设置的值进行比较(因为您忘记了“.gif”和html可能添加的任何垃圾,因为它是一个链接)


更好?

缩进代码。这将使阅读更容易。请只提供重要的内容…我会记住这一点,谢谢。我在以前的问题中只提供了少量代码,没有答案解决了我的问题。我的建议,从头开始。了解最新情况,一步一步地进行,确保您理解概念,并且以正确的方式进行操作。JS非常特别,如果你不知道自己做错了什么,代码就会变得一团糟(正如你所见)
slotitem = new Array('number1test', 'number2test', 'number3test'); // create array for each slot item image

tokens = 100; // starting tokens

function stopplay() { // call function

    if (document.slots.UserTokens.value < tokens) // if usertokens are less than value..
    {
        alert("You lost all your tokens")

    } else // otherwise, how much the user gained
    {
        alert("You gained " + (document.slots.UserTokens.value - tokens) + " Token pieces.   ");
    }
}

function rollem() {

    if (Math.floor(document.slots.UserTokens.value) < Math.floor(document.slots.bet.value)) {
        alert("Your bet is larger than your token amount")

    }

    if (document.slots.bet.value > 1) {
        document.slots.banner.value = "Bet is " + document.slots.bet.value + " UserTokens pieces";
    } else {
        document.slots.banner.value = "Bet is " + document.slots.bet.value + " UserTokens piece";
    }

    counter = 0;
    spinem();

}

function spinem() { // speed of randomly generated pictures

    turns1 = 10 + Math.floor((Math.random() * 5))

    for (a = 0; a < turns1; a++)

    {
        document.slots.slot1.src = "" + slotitem[a % 3] + ".gif";
    }

    turns2 = 10 + Math.floor((Math.random() * 5))

    for (b = 0; b < turns2; b++)

    {
        document.slots.slot2.src = "" + slotitem[b % 3] + ".gif";
    }

    turns3 = 10 + Math.floor((Math.random() * 5))

    for (c = 0; c < turns3; c++)

    {
        document.slots.slot3.src = "" + slotitem[c % 3] + ".gif";
    }

    counter++;

    if (counter < 25) {
        setTimeout("spinem(counter);", 50);
    } else {
        checkmatch();
    }

}

function checkmatch() {
    // the problem is here, where the cases seem to never happen, the program just goes to the else statement, and when one case is true on the webpage, it doesnt display anything(Nor the else statement)
    if ((document.slots.slot1.src == document.slots.slot2.src && document.slots.slot2.src == document.slots.slot3.src))
        switch (document.slots.slot1) {
        case "number1test":
            document.slots.banner.value = ("You got 3 in a row for 1's") // Do stuff
            break;
        case "number2test":
            document.slots.banner.value = ("You got 3 in a row for 2's")
            break;
        case "number3test":
            document.slots.banner.value = ("You got 3 in a row for 3's")
            break;
    } else {
        document.slots.UserTokens.value = document.slots.UserTokens.value - document.slots.bet.value;
        document.slots.banner.value = "No match - You lost " + document.slots.bet.value + " Token piece(s)";
    }
}
alert(document.slots.slot1.src);