以前の質問では問題を解決する答えが得られなかったため、問題を特定するためにコード全体を投稿することにしました。問題は、ボタンが押されたときです。たとえば、3 つの画像の 1 つが同じ (他のすべてと等しい) (3 つの 1 が表示されるか、3 つの 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)";
}
}