简体   繁体   English

Javascript/HTML DOM:如何检查文本用户输入是否等于 if 语句中的字符串

[英]Javascript/HTML DOM: How do I check if text user input equals to string in if statement

I am trying to check in my if statement if the text the user inputs from my html form equals to the string I indicate when submit is clicked.如果用户从我的 html 表单输入的文本等于我在单击提交时指示的字符串,我正在尝试检查我的 if 语句。 I tried using regular expressions to check if the text is a string, but not sure if done properly.我尝试使用正则表达式来检查文本是否为字符串,但不确定是否正确完成。 Both if statements end up going straight to the "else alert()" when submit is clicked.当单击提交时,两个 if 语句最终都会直接进入“else alert()”。

 document.addEventListener('DOMContentLoaded', () => { const squares = document.querySelectorAll('.grid div') const result = document.querySelector('#result') const displayCurrentPlayer = document.querySelector('#current-player') let currentPlayer = 1 let colorSubmit = document.getElementById("color"); colorSubmit.addEventListener('submit', pickColor); function pickColor(square) { let colorStatus = isColorString(this.player1.value) //let x = document.getElementById("player1"); //var x = document.getElementById("player1").value; if (colorStatus === "red") { square.classList.add('player-red') } else if (colorStatus === "yellow") { square.classList.add('player-yellow') } else alert('pick "red" or "yellow"') let colorStatus2 = isColor2String(this.player2.value) //let y = document.getElementById("player2"); //var y = document.getElementById("player2").value; if (colorStatus2 === "blue") { square.classList.add('player-blue') } else if (colorStatus2 === "black") { square.classList.add('player-black') } else alert('pick "blue" or "black"') } function isColorString(player1) { const re = new RegExp('[az]+'); return re.test(player1); } function isColor2String(player2) { const re = new RegExp('[az]+'); return re.test(player2); }
 <form id="color"> <label for="player1">Player 1 Color:</label> <input type="text" id="player1" name="player1"><br><br> <label for="player2">Player 2 Color:</label> <input type="text" id="player2" name="player2"><br><br> <input type="submit" value="Submit"> </form>

Using double equal instead of triple equal may solve your problem.使用双等号而不是三等号可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript:如何检查 DOM 输入值作为 if 语句中的参数 - Javascript: How can I check DOM input value as a parameter in if statement 如何检查用户提交的文本输入中的特定字符串? - How do I check for specific string within text input submitted by a user? 如何结合用户输入更改文本方法和if语句? - How do i combine user input change text method and an if statement? 如何根据我的 html 输入创建一个 javascript if 语句? - How do I create a javascript if statement depending on my html input? 如何检查字符串是否等于 Javascript 中的数组条目? - How can I check if a string equals an array entry in Javascript? 如何使用 JavaScript 对文本输入执行验证检查? - How do I perform a validation check to a text input using JavaScript? Javascript - 当我的推荐人等于 x 时,我如何显示一串文本 - Javascript - How do I show a string of text when my referrer equals x 如何在某些父DOM元素的html字符串中包含用户输入? - How to include user input in the html string of a some parent DOM element? 如何在不使用 JavaScript 库的情况下检查 DOM 的 HTML 组件何时加载? - How do I check when the HTML component of the DOM has loaded without using a JavaScript library? 如何测试字符串类型是否等于另一个JavaScript - How do I test if a string kind of equals another javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM