自动的计算

<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还是蛮重要的。。。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值