我的问题可能存在于保险单选按钮中,并将其值解释为calculate()函数。我的目标是获取每个选项的价值(紧凑=每天30美元,中级=每天40美元,标准=每天50美元)将这些值乘以租车的天数,并考虑是否是客户购买了给定车辆上的保险(保险=每天+ 15美元,没有保险=每天+0美元)我无法正确编码我需要的功能。
<script type="text/javascript">
function calculate() {
var carType = document.getElementById('selectCar').value;
var days = document.getElementById('daysRenting').value;
var yInsurance = document.getElementById('yesInsurance');
var nInsurance = document.getElementById('noInsurance');
var result = (carType * days * yInsurance * nInsurance);
totalCost.value= result;
}
</script>
</head>
<body>
<div align="center">
<form name="carForm">
<p>
<strong>
<h1>Car Rental Service</h1></strong></p>
<hr>
<select name='selectCar' onchange="calculate(this.value)">
<option value="0">Pick A Car Type</option>
<option value="30" id='compact'>Compact</option>
<option value="40" id='intermediate'>Intermediate</option>
<option value="50" id='standard'>Standard</option>
</select>
<hr>
<input type="text" id="daysRenting" size="41px" placeholder="How many days are you renting this vehicle?"/>
<hr>
I want insurance on this vehicle.<input type="radio" value='15' name='insurance' id="yesInsurance"><br>
I do not want insurance on this vehicle.<input type="radio" value='0' name='insurance' id="noInsurance">
<hr>
<input type="button" onclick="calculate()" value="Click here to process request"><br><br>
Total Cost to Rent: <input type="number" id='total' name='totalCost' size=8 readonly><br><br>
<input type="reset" value="Reset"><br>
<hr>
</form>
</div>
答案 0 :(得分:1)
你有几个错误:
selectCar
var carType = document.getElementById('selectCar').value;
totalCost
未定义(var totalCost = document.getElementById('total');
)var result = (carType * days * yInsurance * nInsurance);
中的错误乘法,因为nInsurance
和yInsurance
是指DOM元素,而不是它们的值,所以它们不能相乘result
始终为0 这是一个固定版本:
function calculate() {
var carType = document.getElementById('selectCar').value;
var days = document.getElementById('daysRenting').value;
var yInsurance = document.getElementById('yesInsurance');
var nInsurance = document.getElementById('noInsurance');
var result = (carType * days);
if (yInsurance.checked)
result += parseInt(yInsurance.value, 10) * days;
var totalCost = document.getElementById('total');
totalCost.value = result;
}
<div align="center"><form name="carForm">
<p><strong><h1>Car Rental Service</h1></strong></p><hr>
<select name='selectCar' onchange="calculate(this.value)" id="selectCar">
<option value="0">Pick A Car Type</option>
<option value="30" id='compact'>Compact</option>
<option value="40" id='intermediate'>Intermediate</option>
<option value="50" id='standard'>Standard</option>
</select><hr>
<input type="text" id="daysRenting" size="41px" placeholder="How many days are you renting this vehicle?"/><hr>
I want insurance on this vehicle.<input type="radio" value='15' name='insurance' id="yesInsurance"><br>
I do not want insurance on this vehicle.<input type="radio" value='0' name='insurance' id="noInsurance"><hr>
<input type="button" onclick="calculate()" value="Click here to process request"><br><br>
Total Cost to Rent: <input type="number" id='total' name='totalCost' size=8 readonly><br><br>
<input type="reset" value="Reset"><br><hr>
</form></div>
答案 1 :(得分:0)
嗯,首先你没有ID为selectCar
的元素 - 你的元素名称不一样。