请帮我编写此表单的脚本。我只需要“总计”框来计算当有人点击单选按钮时。我从另一个问题中得到了部分答案,我试着按照答案但是没有用。我显然缺少其他东西,而且我对JS的了解不够先进,无法弄明白。你能查看我的代码并告诉我我错过了什么或做错了吗?谢谢。(我为凌乱的格式道歉)
<html>
<head>
<title> HTML and JavaScript </title>
<script type="text/javascript">
function towerPic()
{}
function doClear()
{
document.AddressForm.customer.value = "";
document.AddressForm.address.value = "";
document.AddressForm.city.value= "";
document.AddressForm.state.value = "";
document.AddressForm.zip.value = "";
document.AddressForm.phone.value= "";
document.AddressForm.email.value= "";
document.AddressForm.total.value= "0.00";
document.ComputerForm.cases[0].checked = false;
document.ComputerForm.cases[1].checked = false;
document.ComputerForm.cases[2].checked = false;
document.ComputerForm.monitors[0].checked = false;
document.ComputerForm.monitors[1].checked = false;
document.ComputerForm.monitors[2].checked = false;
document.ComputerForm.printers[0].checked = false;
document.ComputerForm.printers[1].checked = false;
document.ComputerForm.printers[2].checked = false;
return;
}
var prices = new Object();
function updateTotal () {
var total = 0.00;
for (var price in prices) {
total += price;
}
document.getElementById("total").value = total;
}
</script>
</head>
<body>
<h1 align="center">Computer System Order Form</h1>
<table border="black" cellpadding="10px" align="center">
<tr>
<td>
<table cellpadding="10px">
<form name="ComputerForm">
<tr>
<td>
Computer Case Style:</br>
<input type="radio" onclick="towerPic()" name="cases" onchange="javascript:prices['cases'] = parseInt(500.00); updateTotal();"id="desktop" value="500.00" /> Desktop Case (500.00) </br>
<input type="radio" onclick="towerPic()" name="cases" onchange="javascript:prices['cases'] = parseInt(600.00); updateTotal();"id="mini" value="600.00" /> Mini-Tower Case (600.00) </br>
<input type="radio" onclick="towerPic()" name="cases" onchange="javascript:prices['cases'] = parseInt(700.00); updateTotal();" id="full" value="700.00" /> Full-Tower Case (700.00) </br>
</td>
<td>
<img src="case.jpg" width="125" height = "125" id="pcCase" >
</td>
</tr>
<tr>
<td>
Computer Monitor: <br/>
<input type="radio" name="monitors" value="250.00" /> 17" LCD Flat Screen (250.00) </br>
<input type="radio" name="monitors" value="300.00" /> 19" LCD Flat Screen (300.00) </br>
<input type="radio" name="monitors" value="350.00" /> 21" LCD Flat Screen (350.00) </br>
</td>
<td>
<img src="monitor.jpg" width="125" height = "125" alt="pc monitor" />
</td>
</tr>
<tr>
<td>
Computer Printer:<br/>
<input type="radio" name="printers" value="100.00" /> Inkjet Printer (100.00) </br>
<input type="radio" name="printers" value="250.00" /> Laser Printer (250.00) </br>
<input type="radio" name="printers" value="350.00" /> Color Printer (350.00) </br>
</td>
<td>
<img src="printer.jpg" width="125" height = "125" alt="pc printer" />
</td>
</tr>
</form>
</table>
</td>
<td>
<table padding="10px">
<tr>
<form name="AddressForm">
<td>Total System Price:</td>
<td>$<input type="text" name="total" readonly value="0.00" size="8"></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Full Name:</td>
<td><input type="text" name="customer" /></td>
</tr>
<tr>
<td>Street Address:</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city" /></td>
</tr>
<tr>
<td>State:</td>
<td><input type="TEXT" name="state" size="2"></td>
</tr>
<tr>
<td>Zip:</td>
<td><input type="text" name="zip" /></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td>Email Address:</td>
<td><input type="text" name="email"></td>
</tr>
</form>
<tr>
<td> </td>
</tr>
<tr>
<td>
<input type="button" value="Submit Order" onClick="doSubmit()" />
</td>
<td>
<input type="button" value="Clear Form" onClick="doClear()" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
答案 0 :(得分:1)
您应该使用ID而不是名称:
$<input type="text" name="total" id="total" readonly value="0.00" size="8">
此外,您可能希望执行以下操作来填充prices
:
<input type="radio" onchange="updateTotal(this.id, this.value)" id="desktop" value="500.00" /> Desktop Case (500.00) </br>
这样您就可以更轻松,更统一地在脚本中存储和使用这些值。