<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function cal(){
var v_amount = document.getElementsByName("amount");
var v_price = document.getElementsByName("price");
var total=0;
for (var i=0;i<v_amount.length;i++){
var temp = parseInt(v_amount[i].value)*parseInt(v_price[i].value);
total +=temp;
}
document.form1.total.value=total;
}
</script>
</head><body>
<form id="form1" name="form1" method="get" action="">
  <label>
  <input name="amount" type="text" id="amount" />
  </label>
  <label>
  <input name="price" type="text" id="price" />
  </label>
  <p>
    <label>
    <input name="amount" type="text" id="amount" />
    </label>
    <label>
    <input name="price" type="text" id="price" />
    </label>
  </p>
  <p>
    <label>
    <input name="amount" type="text" id="amount" />
    </label>
    <label>
    <input name="price" type="text" id="price" />
    </label>
  </p>
  <p>
    <label>
    <input name="total" type="text" id="total" />
    </label>
  </p>
  <p>
    <label>
    <input type="submit" name="Submit" value="提交" onclick="cal(); return false;" />
    </label>
  </p>
</form>
</body>
</html>
对表单内容进行求和,但当有某个text框内值为空的时候,总和(total)就为NaN
所有text框都填满时就OK。
求解!!