作为我之前的问题,我无法得到任何解决我问题的答案,我决定发布我的整个代码来确定问题。问题是当按下按钮时,即使三个图像中的一个是相同的(等于所有其他图像)例如(出现三个 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(在末尾<body>
):
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)";
}
}