Html引用计算 - javascript

时间:2013-04-15 22:09:08

标签: javascript html

我正在尝试计算我的html表单值。

我尝试了以下javascript计算,但它没有计算我的总字段值('total1')。可能是javascript存在问题,还是我没有正确引用表单?

    <form action="" method="get" id="quote">

<table width="532" border="1" cellspacing="1" cellpadding="0.5">
  <tr>
    <th width="109" scope="col"><div align="center">Date</div></th>
    <th width="158" scope="col"><div align="center">Half day</div></th>
    <th width="112"><div align="center">Full day</div></th>
    <th width="134"><div align="center">Total for the day</div></th>
  </tr>
  <tr>
    <td><div align="center">
      <input name="date1" type="text" size="15" />
    </div></td>
    <td><div align="center">
      <input name="halfday1" type="text" size="15" maxlength="10" />
    </div></td>
    <td><div align="center">
      <input name="fullday1" type="text" size="15" />
    </div></td>
    <td><div align="center">
      <input name="total1" type="text" size="15" />
    </div></td>
  </tr>
  <tr>
    <td><div align="center">
      <input name="date2" type="text" size="15" />
    </div></td>
    <td><div align="center">
      <input name="halfday2" type="text" size="15" />
    </div></td>
    <td><div align="center">
      <input name="fullday2" type="text" size="15" />
    </div></td>
    <td><div align="center">
      <input name="total2" type="text" size="15" />
    </div></td>
  </tr>
  <tr>
    <td><div align="center">
      <input name="date3" type="text" size="15" />
    </div></td>
    <td><div align="center">
      <input name="halfday3" type="text" size="15" />
    </div></td>
    <td><div align="center">
      <input name="fullday3" type="text" size="15" />
    </div></td>
    <td><div align="center">
      <input name="total3" type="text" size="15" />
    </div></td>

  </tr>
  <tr>
    <td><div align="center"></div></td>
    <td><div align="center"></div></td>
    <td><div align="center"></div></td>
    <td><div align="center"></div></td>
  </tr>
</table>

</form>



      function doTotal(quote) {
    var a = (form.halfday1.value != '') ? eval(form.halfday1.value) : 0;
    var b = (form.fullday1.value != '') ? eval(form.fullday1.value) : 0;


    form.total1.value = a + b + c + d + e;
}

http://jsfiddle.net/ZR5LD/

1 个答案:

答案 0 :(得分:1)

http://jsfiddle.net/ZR5LD/2/

function doTotal(form) {
    var a = (+form.halfday1.value);
    var b = (+form.fullday1.value);


    form.total1.value = a + b ;
}
document.getElementById("quote").onclick = function (){doTotal(this)};

我不知道c,d,e是什么,但form没有定义,我假设你的意思是"quote"。 我还在表格

doTotal属性中添加了对onclick的调用