gpt4 book ai didi

javascript - 我的 JavaScript 有什么问题吗?它不会计算和显示我想要的值;这让我发疯!

转载 作者:行者123 更新时间:2023-11-28 16:33:04 24 4
gpt4 key购买 nike

我的 JavaScript 有什么问题吗?

<?xml version = "1.0" encoding ="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1 /DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

...

<body>
<p>
<a href="http://nova.umuc.edu/~ct386a28/handson/index.html">Main Page</a>
</p>
<h1>GAS MILEAGE CALCULATOR</h1>

<form action="" method="post">

<script type = "text/javascript">

function mileageCalculator (beginMil, endMil, galCons, form)
{

var bM = parseFloat(beginMil);
var eM = parseFloat(endMil);
var gC = parseFloat(galCons);

if((eM<0)||(bM<0)||(gC<0))
{
alert("ERROR: One or more input(s) is negative.");
form.begMileage = " ";
form.endMileage = " ";
form.galConsumed = " ";
}

else if ((eM == 0) || (gC == 0))
{
alert("ERROR: The end mileage and/or gallon input is zero.");
form.begMileage = " ";
form.endMileage = " ";
form.galConsumed = " ";
}

else if (eM < bM)
{
alert("ERROR: End mileage is less than begining mileage.");
form.begMileage = " ";
form.endMileage = " ";
form.galConsumed = " ";
}
else
{
form.mpg.value = ((bM - eM) / gC);

}
}

</script>



<div class = "center">
<h3>Enter beginning mileage:</h3>
<input type="text" name ="begMileage" tabindex="1" />
<br />
<h3>Enter ending mileage:</h3>
<input type="text" name ="endMileage" tabindex="2" />
<br />
<h3>Enter gallons consumed:</h3>
<input type="text" name ="galConsumed" tabindex="3" />
<br />

<h2>TOTAL MILES PER GALLON</h2>
<input type="text" name ="mpg" tabindex="4" /> <br />

</div>

<p><br /></p>

<p>
<br /><input type="submit" value="SUBMIT" onclick = "mileageCalculator"/> <input type="reset" value="CLEAR"/> <br />
</p>

</form>

最佳答案

您不能只向函数提交表单。您必须使用某种 DOM 方法获取表单字段值并将其传递到您的函数中。反之亦然,您必须访问要输出的表单字段并设置值。

尽管这有点过时,但这很好地遵循了您的模式。

http://www.javaworld.com/jw-06-1996/jw-06-javascript.html

关于javascript - 我的 JavaScript 有什么问题吗?它不会计算和显示我想要的值;这让我发疯!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5162558/

24 4 0
文章推荐: python - 如何重用使用 unittest.testcase 编写的测试
文章推荐: javascript - 如何使用 Javascript 链接到外部
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com