首页 > 解决方案 > 如何使用大写/小写

问题描述

嘿所以我有一个这样的代码

const answer = "HA"

const answers = answer.toLowerCase();

const theanswer = document.getElementById("WIT").value;

if(theanswer == answers) {
console.log("correct")
}

问题是只有当我输入“ha”时才会出现正确的

如果我输入“HA”或“Ha”或“hA”或“ha”,我怎样才能做出正确的显示

标签: javascripthtml

解决方案


要比较这些值 - 也将值转换为小写 - 例如 - 尝试在输入中键入“ha”。

在第一个字符 - “h” - 您将记录“h:不正确”,然后在“a”上记录“ha:正确”。任何其他字符 s 显然与“HA”不匹配,并且是不正确的。

const answer = "HA"
const answers = answer.toLowerCase();

document.querySelector('#WIT').addEventListener('keyup', checkMe)

function checkMe(){
  const theanswer = document.getElementById("WIT").value;

if(theanswer.toLowerCase() == answers) {
  console.log(theanswer + ": correct")
  } else {
  console.log(theanswer + ": incorrect")
  }
}
<input type="text" id="WIT" />


推荐阅读