如何在HTML中编写用户输入文本框,并使用javascript显示解释,如果答案正确,否则说不正确



我想为我的网站编写代码,以便只有在用户输入正确的情况下才能显示问题的解释,否则,它应该说我不正确。我想知道我需要什么功能以及如何编写HTML5部分。

你想要这样的东西吗?

let answer = "washington";
const input = document.getElementById("input");
const button = document.getElementById("checkAnswer")
button.addEventListener("click", () => {
if (input.value.toLowerCase() === answer) {
alert("you are right")
} else {
alert("you are incorrect")
input.value = ""
}
})
<p>where is the capital of america?</p>
<label for="input">Your answer</label>
<input type="text" id="input">
<button type="button" id="checkAnswer">Check</button>

相关内容

最新更新