JavaScript简单合计功能 数量x单价

<script type="text/javascript">
function anHeJi()
{
  var count,cheng;
  count=0;
  for(var i=1;i<=3;i++)
  {
    cheng = document.getElementById("num"+i).value * document.getElementById("price"+i).value;
    count += cheng; 
  }
  document.form.total.value = count;
}
</script>
<form name="form">
  <p>
    数量:<input type="text" name="num1" id="num1" onKeyUp="anHeJi();" />
    单价:<input type="text" name="price1" id="price1" onKeyUp="anHeJi();" />
  </p>
  <p>
    数量:<input type="text" name="num2" id="num2" onKeyUp="anHeJi();" />
    单价:<input type="text" name="price2" id="price2" onKeyUp="anHeJi();" />
  </p>
  <p>
    数量:<input type="text" name="num3" id="num3" onKeyUp="anHeJi();" />
    单价:<input type="text" name="price3" id="price3" onKeyUp="anHeJi();" />
  </p>
  <p>合计:<input type="text" name="total" id="total" /></p>
</form>
posted @ 2012-12-03 13:46  Al-one  阅读(616)  评论(0编辑  收藏  举报