【问题标题】:Javascript to check a textbox for a certain wordJavascript 检查文本框中的某个单词
【发布时间】:2017-11-02 02:38:36
【问题描述】:

好的,所以我正在尝试为初级作业制作一个小的 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") > -1)

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

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

【问题讨论】:

    标签: javascript html function if-statement textbox


    【解决方案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>

    希望这会有所帮助! :)

    【讨论】:

    • 谢谢,这对以后的使用很有帮助,但对当前的问题没有帮助。不过非常感谢! :)
    【解决方案2】:

    首先,您的nextPuzzle 函数中需要一个}; 其次,如果你想使用onclick DOM 事件,你应该在头部定义函数,或者你可以使用addEventListener 来绑定事件处理程序。

    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);
    <!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>

    【讨论】:

    • 好的,所以我在我的代码中这样做了,但 if 语句仍然不起作用......还有其他想法吗?另外,感谢您抽出宝贵时间回答。
    • 您的单词是否检查了需求是否区分大小写?
    猜你喜欢
    • 2017-09-09
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多