我在下面输入了以下内容。我试图运行一个while
循环,以允许用户仅输入正数并将其显示在上的三个输入框中myForm
。我reset
在表单中添加了以下内容,以便清除表单:<input type="reset" onclick="myForm" value="Reset the form" ></input>
。它清除了表单,但是当我单击输入框以添加新变量时,旧变量重新出现,并且不提示输入新输入。如何更正代码以解决此问题?我在下面添加了带有注释的代码,以帮助显示我正在尝试创建的内容。
first = -1;
second = -1;
third = -1;
function getValues() {
//while loop to not allow negative numbers or strings//
while (isNaN(first) || first == "" || first < 0) {
first = prompt("Enter the first number.");
break;
}
//while loop to not allow negative numbers or strings//
while (isNaN(second) || second == "" || second < 0) {
second = prompt("Enter the second number.");
break;
}
//while loop to not allow negative numbers or strings//
while (isNaN(third) || third == "" || third < 0) {
third = prompt("Enter the third number.");
break;
}
document.myForm.first.value = first;
document.myForm.second.value = second;
document.myForm.third.value = third;
}
function determineLarge() {
let message = "";
//if statement to determine first number is the largest//
if (second < first && third < first) {
message = "The first number" + "(" + first + ")" + " is larger.";
}
// else if statement to determine second number is the largest//
else if (first < second && third < second) {
message = "The second number" + "(" + second + ")" + " is larger.";
}
//else if statement to determine the third number is the largest//
else if (first < third && second < third) {
message = "The third number " + "(" + third + ")" + " is larger.";
}
//else if statement to determine the first number as the largest number if any values are equal//
else if (first == second || second == first || first == third || second == third) {
message = "The first number" + "(" + first + ")" + " is largerer.";
}
document.getElementById("results").innerHTML = message;
//fucntion to reset form//
function myForm() {
document.getElementById("myForm").reset();
}
}
<body>
<h1>Gary's Largest of Three Numbers</h1>
<!-- estasblishing form name-->
<form name="myForm">
<p> Enter the first number</p>
<!--prompts user for first input-->
<input type="number" name="first" value="" onclick="javascript:getValues();">
<p>Enter the second number</p>
<!--prompts user for second input-->
<input type="number" name="second" value="" onclick="javascript:getValues();">
<p>Enter the third number</p>
<!--prompts user for third input-->
<input type="number" name="third" value="" onclick="javascript:getValues()">
<!--runs the function to determine the largest number-->
<button type="button" onclick="determineLarge();">Determine the larger number</button>
<!--resets the form so user can insert different numbers-->
<input type="reset" onclick="myForm" value="Reset the form"></input>
</form>
<div id="results"> </div>
这里有很多问题。首先,这看起来像是一个简单的普通错字:onclick="myForm"
。需要明确的是,这不是在调用包含调用的myForm()
函数reset()
。你需要父母。
其次,解决此问题揭示了另一个问题:由于使用type="reset" onclick="myForm"
,因此发生了重置,这是一种重置表单的方法。(来源:Developer.Mozilla.org。)我需要将myForm()
函数简单地重命名为resetForm()
,以便oncall=
可以在重命名后实际运行。
第三,您在该元素上没有id,因此getElementById()
最好不要返回任何内容,因此请尝试:document.getElementsByName("myForm")[0].reset();
。或者,您可以添加ID并继续使用getElementById()
自己选择的。
第四,您需要重置绑定到这三个值的内部变量,以便getValues()
函数可以看到正确的数据。
看一看:
first = -1;
second = -1;
third = -1;
function getValues(){
//while loop to not allow negative numbers or strings//
while(isNaN(first) || first == "" || first < 0){
first = prompt ("Enter the first number.");
break;
}
//while loop to not allow negative numbers or strings//
while(isNaN(second) || second == "" || second < 0){
second = prompt ("Enter the second number.");
break;
}
//while loop to not allow negative numbers or strings//
while(isNaN(third) || third == "" || third < 0){
third = prompt ("Enter the third number.");
break;
}
document.myForm.first.value = first;
document.myForm.second.value = second;
document.myForm.third.value = third;
}
function resetForm(){
document.getElementsByName("myForm")[0].reset();
first = -1;
second = -1;
third = -1;
}
function determineLarge(){
let message = "";
//if statement to determine first number is the largest//
if (second < first && third < first){
message = "The first number" + "(" + first + ")" + " is larger.";
}
// else if statement to determine second number is the largest//
else if (first > second && third > second) {
message = "The second number" + "(" + second + ")" + " is larger.";
}
//else if statement to determine the third number is the largest//
else if (first > third && second > third){
message = "The third number " + "(" + third + ")" + " is larger.";
}
//else if statement to determine the first number as the largest number if any values are equal//
else if (first == second || second==first || first==third || second ==third){
message = "The first number" + "(" + first + ")" + " is largerer.";
}
document.getElementById("results").innerHTML = message;
}
<body>
<h1>Gary's Largest of Three Numbers</h1>
<!-- estasblishing form name-->
<form name="myForm" >
<p> Enter the first number</p>
<!--prompts user for first input-->
<input type="number" name="first" value="" onclick="javascript:getValues();">
<p>Enter the second number</p>
<!--prompts user for second input-->
<input type="number" name="second" value="" onclick="javascript:getValues();">
<p>Enter the third number</p>
<!--prompts user for third input-->
<input type="number" name="third" value="" onclick="javascript:getValues()">
<!--runs the function to determine the largest number-->
<button type="button" onclick="determineLarge();">Determine the larger number</button>
<!--resets the form so user can insert different numbers-->
<input type="button" onclick="resetForm();" value="Reset the form" ></input>
</form>
<div id="results"> </div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句