正如我以前的問題,我沒有得到任何解決我的問題的答案,我決定發佈我的整個代碼來確定問題。問題是,當按下按鈕時,即使三個圖像中的一個圖像相同(等於所有其他圖像)(例如三個1出現,或三個2出現),我輸入的程序中沒有輸入任何消息,而是輸出發生。即使條件成立,Javascript切換工具箱也不起作用
這是我原來的問題(Switch case not working for Images stored in array)
的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)";
}
}
縮進你的代碼。這會讓閱讀變得更容易。 – Blender
並只給重要的東西,請... – thinklinux
我會記住這一點,謝謝。 – user2396940