gpt4 book ai didi

javascript - 单选按钮值在计算中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 18:17:52 24 4
gpt4 key购买 nike

我正在尝试获取 bat mobile、gauss hog 和 light cycle 的单选按钮来计算成本。其他计算适用于其他变量但不适用于 radCar。

    <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Cars4You Car Rentals</title>
<link rel="stylesheet" type="text/css" href="mystylesheet.css">
<script type="text/javascript">
function calculateCost()
{
var cost = 0
var radCar;
var chkGPS = 0
var rentalDay = parseInt(document.rentalForm.rentalDay.value);
var insurance = 0

radCar = document.rentalForm.radCar.value

if (radCar=="batmobile")
{
cost = 100
}
if (radCar=="gausshog")
{
cost = 85
}
if (radCar=="lightcycle")
{
cost = 40
}

if ( document.rentalForm.chkGPS.checked )
{
chkGPS = chkGPS+5;
}

if ( document.rentalForm.insurance.value=="Yes" )
{
insurance = insurance+20;
}

if (document.rentalForm.rentalDay.value>=7)
{
cost = cost-10;
}

finalPrice = (cost + chkGPS)*rentalDay + insurance;
alert("Your rental cost is $" + finalPrice);
}
</script>
</head>
<body>
<div id="header">
<h1>Cars4You Car Rentals</h1>
<a href="home.html" target="_blank">Home</a>
<----------------------------------------------------->
<a href="contact.html" target="_blank">Contact</a>
</div>
<div id="main_content">
<form name=rentalForm>
First Name:
<input type="text" name="txtFirstName"> <br>
Last Name:
<input type="text" name="txtLastName"> <br>
<h2>Vehicle Type</h2>
Batmobile ($100/day) :
<input type="radio" name="radCar" value="batmobile"> <br>
Gausshog ($85/day) :
<input type="radio" name="radCar" value="gausshog"> <br>
Lightcycle ($40/day) :
<input type="radio" name="radCar" value="lightcycle"> <br><br>
How many rental days?
<select name="rentalDay">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option> <br>
</select> <br>
*$10 discount for 7 or more days <br>
<h2>Extras</h2>
Insurance ($20)?
<select name="insurance">
<option value="Yes">Yes</option>
<option value="No">No</option>
</select> <br>
GPS:
<input type="checkbox" name="chkGPS"> <br>
Special Requests: <br>
<textarea style="width:200px" name="txtarRequests" rows=5></textarea> <br>
<input type="button" name="btnsubmit" value="Book My Rental!" onclick="calculateCost()">
</div>
</form>
</body>
</html>

最佳答案

您需要一个函数来从一组 radio 中获取选中的 radio 的值。

替换

radCar = document.rentalForm.radCar.value

radCar = getRadio('radCar');

参数在哪里,在本例中为 radCar是 radio 的名称,即 <input type="radio" name="radCar"

这是函数

function getRadio(name) {
var radios = document.getElementsByName(name);
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) return radios[i].value;
}
}

工作示例 http://jsfiddle.net/VX9Ge/

旁白:我发现 switch 比 if/elseif/else 丛林要干净得多。

switch (radCar){
case "batmobile":
cost=100;
break;
case "gausshog":
cost=85;
break;
case "lightcycle":
cost=40;
break;
default: // none selected
cost=0;
}

关于javascript - 单选按钮值在计算中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22432015/

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