<html>
<head>
<title>自动计算</title>
<script language="javascript">
function calculate()
{
if(document.getElementById("zidong").checked)
{
document.getElementById("totalmoney").value = parseInt(document.getElementById("number").value)*parseInt(document.getElementById("price").value);
document.getElementById("zongji").value = document.getElementById("totalmoney").value;
}
if(document.getElementById("guangpan").checked)
{
document.getElementById("zongji").value = parseInt(document.getElementById("totalmoney").value) + parseInt(document.getElementById("guangpan").value);
}
if(document.getElementById("yingpan").checked)
{
document.getElementById("zongji").value = parseInt(document.getElementById("totalmoney").value) + parseInt(document.getElementById("yingpan").value);
}
if(document.getElementById("xianshiqi").checked)
{
document.getElementById("zongji").value = parseInt(document.getElementById("totalmoney").value) + parseInt(document.getElementById("xianshiqi").value);
}
if(document.getElementById("guangpan").checked && document.getElementById("yingpan").checked)
{
document.getElementById("zongji").value = parseInt(document.getElementById("totalmoney").value) + parseInt(document.getElementById("guangpan").value) + parseInt(document.getElementById("yingpan").value);
}
if(document.getElementById("yingpan").checked && document.getElementById("xianshiqi").checked)
{
document.getElementById("zongji").value = parseInt(document.getElementById("totalmoney").value) + parseInt(document.getElementById("yingpan").value) + parseInt(document.getElementById("xianshiqi").value);
}
if(document.getElementById("guangpan").checked && document.getElementById("xianshiqi").checked)
{
document.getElementById("zongji").value = parseInt(document.getElementById("totalmoney").value) + parseInt(document.getElementById("guangpan").value) + parseInt(document.getElementById("xianshiqi").value);
}
if(document.getElementById("guangpan").checked && document.getElementById("yingpan").checked && document.getElementById("xianshiqi").checked)
{
document.getElementById("zongji").value = parseInt(document.getElementById("totalmoney").value) + parseInt(document.getElementById("guangpan").value) + parseInt(document.getElementById("yingpan").value) + parseInt(document.getElementById("xianshiqi").value);
}
}
</script>
</head>
<body>
<p align="center" style="color:red">
个人电脑订购单:
</p>
<p>
<hr>
</p>
<p align="left">
数量:<input type="text" id="number" οnchange="calculate()">
价格:<input type="text" id="price" οnchange="calculate()">
</p>
<p align="left">
小计:<input type="text" id="totalmoney" οnclick="calculate()">
</p>
<p align="left">
选购项目:
<input type="checkbox" id="guangpan" value="500" οnchange="calculate()">光驱500元
<input type="checkbox" id="yingpan" value="500" οnchange="calculate()">硬盘500元
<input type="checkbox" id="xianshiqi" value="1500" οnchange="calculate()">显示器1500元
</p>
<p>
<input type="checkbox" id="zidong" checked οnclick="if(this.checked){calculate();}">自动计算
</p>
<p>
总计:<input type="text" id="zongji">
</p>
</body>
</html>
根据网上的例子简单写了一遍,觉得javascript还是蛮重要的。。。