javascript与indexOf有问题

时间:2015-11-08 14:16:08

标签: javascript html

所以我有一个html页面,猜猜我的文字游戏。但当我输入FinalGuess变量indexOf的第一个if语句时,它会中断,我在哪里使用SecretWord页面有效,但函数没有。任何想法?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Guessing Game</title>
<script type="text/javascript" >
/* <![CDATA[ */
var SecretWord = "compaq";
var GuessInput = document.forms[0].Guess.text;
var FinalGuess = GuessInput.toLowerCase();

var firstLetter = false;
var b = false;
var c = false;
var d = false;
var e = false;
var f = false;

function checkLetter() {

if (SecretWord.indexOf('c') >= 0) {
firstLetter == true;    
}
/*
if (userInput.indexOf('o') >= 0) {
b == true;  
}
if (userInput.indexOf('m') >= 0) {
c == true;  
}
if (userInput.indexOf('p') >= 0) {
d == true;  
}
if (userInput.indexOf('a') >= 0) {
e == true;  
}
if (userInput.indexOf('q') >= 0) {
d == true;  
}
*/  
if (firstLetter == false) {
var letter1 = "*";
} else {
var letter1 = "c";
}
if (b == false) {
var letter2 = "*";
} else {
var letter2 = "o";
}

if (c == false) {
var letter3 = "*";
} else {
var letter3 = "m";
}

if (d == false) {
var letter4 = "*";
} else {
var letter4 = "p";
}

if (e == false) {
var letter5 = "*";
} else {
var letter5 = "a";
}

if (f == false) {
var letter6 = "*";
} else {
var letter6 = "q";
}

var string = letter1 + letter2 + letter3 + letter4 + letter5 + letter6;

document.forms[0].word.value = string;
document.forms[0].Guess.value = "";
document.forms[0].Guess.focus;
alert(GuessInput);
}
/* ]]> */
</script>
</head>

<body onload="checkLetter();">

<form action="" >
Guess the word
<input type="text" name="word" id="word" /><br />
Enter text here
<input type="text" name="Guess" maxlength="1" />
<input type="button" name="Submit" value="Submit" onclick="checkLetter();" />

</form>
</body>
</html>

1 个答案:

答案 0 :(得分:5)

你必须替换:

firstLetter == true; 

通过:

firstLetter = true;

我们使用==进行比较,但是当您只想分配值时使用=