-2

我正在尝试计算我的 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/

4

1 回答 1

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没有定义,我假设你的意思是 form "quote"doTotal我还在表单的onclick属性中添加了调用

于 2013-04-15T22:25:26.617 回答