用于检查特定单词的文本框的Javascript

时间:2017-11-02 02:38:36

标签: javascript html function if-statement textbox

好吧所以我正在尝试为初级作业制作一个小小的Javascript拼图,而且我只用过文本框中的数字检查,所以请原谅我,如果这看起来有点愚蠢。

无论如何,我要做的是检查一个文本框中的某个单词,一个密码,然后进入下一个“级别”

我的代码:

var timesClicked = 100;

//Function to run the window alert a certain amount of times, as well as hide a certain answer
function changeFunc() {
	
	if (timesClicked > 0){

		if (timesClicked == 50) {
			window.alert("N34T");
			timesClicked--;
			changeFunc();
		}

		else {
			window.alert("NEAT");
			timesClicked--;
			changeFunc();
		}
	}
	else{

		window.alert("Sorry, no more answers may be dispensed at this time!");
	}
}

//Function to go to the next puzzle
function nextPuzzle(){
	
	if (document.getElementById('answer').value.indexOf("N34T") > -1) {
		window.alert("Good Job");
		window.location.href="puzzle02.html";
	}
}
<!DOCTYPE html>
<html>
	<head>
		<script type="text/javascript" src="puzzle01.js">
		</script>
		
		<title>Puzzle 1</title>
	</head>
	<header>
	<h1>Puzzle 1!</h1>
		<p> Please enjoy this puzzle's easiness, they will get harder!</p><br>
	</header>
	<body>
		<p>

		<input type="text" id="answer">
		<button type="button" onclick="nextPuzzle()">Next Puzzle</button><br><br>
		
		<button type="button" onclick="changeFunc()">Answers!</button>
		</p>
		
	</body>
</html>

本声明:

  

if(document.getElementById('answer')。value.indexOf(“N34T”)&gt; -1)

不会在文本框中使用正确的单词运行。

有人可以帮忙吗?感谢任何可以提前帮助的人!

2 个答案:

答案 0 :(得分:1)

假设您在独立页面上有第二个谜题(比方说,puzzle2.html),您只需将其分配给'Good Job'条件中的 window.location

window.location = "puzzle2.html";

这可以在以下内容中看到:

var timesClicked = 100;

//Function to run the window alert a certain amount of times, as well as hide a certain answer
function changeFunc() {
  if (timesClicked > 0) {
    if (timesClicked == 50) {
      window.alert("N34T");
      timesClicked--;
      changeFunc();
    } else {
      window.alert("NEAT");
      timesClicked--;
      changeFunc();
    }
  } else {
    window.alert("Sorry, no more answers may be dispensed at this time!");
  }
}

//Function to go to the next puzzle
function nextPuzzle() {
  if (document.getElementById('answer').value.indexOf("N34T") > -1) {
    window.alert("Good Job");
    window.location = "puzzle2.html";
  }
}
<body>
  <p>
    <input type="text" id="answer">
    <button type="button" onclick="nextPuzzle()">Next Puzzle</button><br><br>
    <button type="button" onclick="changeFunc()">Answers!</button>
  </p>
</body>

希望这有帮助! :)

答案 1 :(得分:0)

首先,}函数需要nextPuzzle; 其次,如果要使用onclick DOM事件,则应在头部定义函数,或者可以将addEventListener用于绑定事件处理程序。

&#13;
&#13;
var timesClicked = 100;

//Function to run the window alert a certain amount of times, as well as hide a certain answer
function changeFunc() {
	
	if (timesClicked > 0){

		if (timesClicked == 50) {
			window.alert("N34T");
			timesClicked--;
			changeFunc();
		}

		else {
			window.alert("NEAT");
			timesClicked--;
			changeFunc();
		}
	}
	else{

		window.alert("Sorry, no more answers may be dispensed at this time!");

	}
			
}



//Function to go to the next puzzle
function nextPuzzle(){
	if (document.getElementById('answer').value.indexOf("N34T") > -1) {
		window.alert("Good Job");
  }
		
}

document.querySelector('.next-puzzle').addEventListener('click', nextPuzzle);
document.querySelector('.change').addEventListener('click', changeFunc);
&#13;
<!DOCTYPE html>
<html>
	<head>
		<script type="text/javascript" src="puzzle01.js">
		</script>
		
		<title>Puzzle 1</title>
	</head>
	<header>
	<h1>Puzzle 1!</h1>
		<p> Please enjoy this puzzle's easiness, they will get harder!</p><br>
	</header>
	<body>
		<p>

		<input type="text" id="answer" />
		<button type="button" class="next-puzzle">Next Puzzle</button><br><br>
		
		<button type="button" class="change">Answers!</button>
		</p>
		
	</body>
	




</html>
&#13;
&#13;
&#13;