gpt4 book ai didi

javascript - 使用 IF 语句输入文本

转载 作者:行者123 更新时间:2023-11-28 14:42:09 25 4
gpt4 key购买 nike

我正在创建一个非常简单的基于故事的“游戏”,其中有一个带有选择的问题(不是单选按钮等),用户必须输入提供给他们的选择。正如你所见,我不太擅长 JavaScript...

<!DOCTYPE HTML>
<html>

<head>
<script type="text/javascript" src="gamescript.js"></script>
</head>


<body>

<p id ="door"> Which door do you wish to enter? Door 1 or Door 2? </p>

<input id="myInput" type="text">

<button onclick="myFunction()">Enter</button>

<script>
function myFunction(){
var door = document.getElementById("myInput").value;
if (door == "Door 1", "door 1"){
document.getElementById("door").innerHTML = "You have entered" + door;
}else if (door == "Door 2", "door 2"){
document.getElementById("door").innerHTML = "You have entered" + door;
}else{
document.getElementById("door").innerHTML = "You must enter a door!"
}
}

</script>

</body>
</html>

我没有收到任何错误,但是当我随机输入一些内容/或留空时,它的意思是“选择一扇门”,但它会显示我在文本框中输入的任何内容。这段代码可能是错误的,我猜它很可能是......我不想上网寻求帮助,但没有其他人可以询问。感谢所有帮助。谢谢。

最佳答案

您的 if 语句的语法是错误的。更改您的 if 语句以使用正确的“or”语法:

function myFunction(){
var door = document.getElementById("myInput").value;
if (door == "Door 1" || door == "door 1"){
document.getElementById("door").innerHTML = "You have entered" + door;
}else if (door == "Door 2" || door == "door 2"){
document.getElementById("door").innerHTML = "You have entered" + door;
}else{
document.getElementById("door").innerHTML = "You must enter a door!"
}
}
<body>

<p id ="door"> Which door do you wish to enter? Door 1 or Door 2? </p>

<input id="myInput" type="text">

<button onclick="myFunction()">Enter</button>


</body>

关于javascript - 使用 IF 语句输入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47642106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com