gpt4 book ai didi

javascript - HTML/Javascript 中的乘法表

转载 作者:行者123 更新时间:2023-12-02 14:13:22 25 4
gpt4 key购买 nike

我想知道我是否离得太远了,我想要一个表格,在本例中是一个乘法表,并让 Javascript 比较结果并显示它是否正确(我意识到这段代码可能对你们中的一些人来说似乎很奇怪,但我对此很陌生,这是一个 self 强加的练习)。

HTML:

<html>

<body>

<form id="myForm">

</h3> 2 * 1 = </h3> <input id="one" type="text" name="1" maxlength="4" size="2"><br>
</h3> 2 * 2 = </h3> <input id="two" type="text" name="2" maxlength="4" size="2"><br>
</h3> 2 * 3 = </h3> <input id="three" type="text" name="3" maxlength="4" size="2"><br>
</h3> 2 * 4 = </h3> <input id="four" type="text" name="4" maxlength="4" size="2"><br>
</h3> 2 * 5 = </h3> <input id="five" type="text" name="5" maxlength="4" size="2"><br>
</h3> 2 * 6 = </h3> <input id="six" type="text" name="6" maxlength="4" size="2"><br>
</h3> 2 * 7 = </h3> <input id="seven" type="text" name="7" maxlength="4" size="2"><br>
</h3> 2 * 8 = </h3> <input id="eigth" type="text" name="8" maxlength="4" size="2"><br>
</h3> 2 * 9 = </h3> <input id="nine" type="text" name="9" maxlength="4" size="2"><br>
</h3> 2 * 10= </h3> <input id="ten" type="text" name="10" maxlength="4" size="2"><br><br>

<input id="submit" type="button" onclick="myFunction()" value="Submit Form">

  <h1 id="results"> Results</h1>

Javascript:

var one = document.getElementById("one").value;
var two = document.getElementById("two").value;
var three = document.getElementById("three").value;
var four = document.getElementById("four").value;
var five = document.getElementById("five").value;
var six = document.getElementById("six").value;
var seven = document.getElementById("seven").value;
var eight = document.getElementById("eight").value;
var nine = document.getElementById("nine").value;
var ten = document.getElementById("ten").value;

var results = document.getElementById("results");

function myFunction() {
var submit = document.getElementById("submit").submit();
if (one === 2 && two === 4 && three === 6 && four === 8 && five === 10 && six === 12 && seven === 14 && eigth === 16 && nine === 18 && ten === 20) {
document.getElementById("results").innerHTML = "Correct!"
} else {
document.getElementById("results").innerHTML = "Try Again!"
}
};

最佳答案

这就是你所期待的

function myFunction() {
var one = document.getElementById("one").value;
var two = document.getElementById("two").value;
var three = document.getElementById("three").value;
var four = document.getElementById("four").value;
var five = document.getElementById("five").value;
var six = document.getElementById("six").value;
var seven = document.getElementById("seven").value;
var eight = document.getElementById("eight").value;
var nine = document.getElementById("nine").value;
var ten = document.getElementById("ten").value;

var results = document.getElementById("results");
var submit = document.getElementById("submit");
if (one == 2 && two == 4 && three == 6 && four == 8 && five == 10 && six == 12 && seven == 14 && eight == 16 && nine == 18 && ten == 20) {
document.getElementById("results").innerHTML = "Correct!"
} else {
document.getElementById("results").innerHTML = "Try Again!"
}
}
h3{
display:inline;
}
<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" href="style.css">

</head>

<body>
<form id="myForm">

<h3> 2 * 1 = </h3> <input id="one" type="text" name="1" maxlength="4" size="2"><br>
<h3> 2 * 2 = </h3> <input id="two" type="text" name="2" maxlength="4" size="2"><br>
<h3> 2 * 3 = </h3> <input id="three" type="text" name="3" maxlength="4" size="2"><br>
<h3> 2 * 4 = </h3> <input id="four" type="text" name="4" maxlength="4" size="2"><br>
<h3> 2 * 5 = </h3> <input id="five" type="text" name="5" maxlength="4" size="2"><br>
<h3> 2 * 6 = </h3> <input id="six" type="text" name="6" maxlength="4" size="2"><br>
<h3> 2 * 7 = </h3> <input id="seven" type="text" name="7" maxlength="4" size="2"><br>
<h3> 2 * 8 = </h3> <input id="eight" type="text" name="8" maxlength="4" size="2"><br>
<h3> 2 * 9 = </h3> <input id="nine" type="text" name="9" maxlength="4" size="2"><br>
<h3> 2 * 10= </h3> <input id="ten" type="text" name="10" maxlength="4" size="2"><br><br>

<input id="submit" type="button" onclick="myFunction()" value="Submit Form">

<h1 id="results"> Results</h1>
</form>
<script src="script.js"></script>
</body>

</html>

关于javascript - HTML/Javascript 中的乘法表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39269353/

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