javascript函数停车时间

javascript function parking hours

本文关键字:时间 停车 函数 javascript      更新时间:2024-06-24

这是完整的问题

写一份完整的表格,根据停车小时数向每位客户收取停车费。要求用户使用prompt()输入总小时数
使用函数calcCharge()计算客户必须支付的费用金额
此功能通过其参数接收总小时数或停车时间。然后,它计算并将费用金额返回给客户
计算客户费用金额的公式:第一个小时的费用为RM 1.00。在接下来的一个小时内,额外收费为每小时0.50令吉。最高收费为RM 10.00。使用函数calcTotal()计算所有客户的总费用。显示客户的费用金额。使用alert()显示所有客户的总费用



我试着编写代码,但没有成功。
我没有显示一个客户的总费用和所有客户的总收费。
我希望有人愿意帮助我,谢谢


<html>
<head><title>Calculate Parking Hours</title>
<script type="text/javascript">
function calcCharge()
{
	var hour=parseInt(document.fee.hour.value);
	var tot;
	if (hour==1)
		tot=1;
	else if (hour>1 && hour<=20)
		tot=1+0.5*(hour-1);
	else
		tot=10;
	
	
	document.write("The total charge is : RM "+tot);
		
}
function calcTotal()
{
	var totCha;
	totCha=parseInt(totCha);
	totCha+=document.tot.value;
	alert("Total charge all customers"+totCha);
}
</script>
</head>
<body onload="calcTotal()">
<form name="fee" method="post">
Hour : <input name="hour" type="text"><br>
<input type="submit" name="submit" value="Total Charge" onclick="calcCharge()">
</form>
</body>
</html>

更新以下行:

var hour = parseInt(document.getElementByName("hour").value);

在您的问题中,您说calcCharge()函数通过其参数接收总小时数或停车时间。事实上,你没有把任何论点传递给你的函数。如果你想用正确的方式,那么试试下面的代码

html

<input type="button"onclick="calcCharge(document.getElementById('text1').value)"/>
<input type="text" id="text1"/>

js

function calcCharge(value)
{
var hour=parseInt(value);
 var tot=0;
if (hour==1)
         tot=1;
    else if (hour>1 && hour<=20)
{
        tot=1+(0.5*(hour-1));
}
    else
{
        tot=10;
}

    alert(tot);
}

DEMO