0

如何在 Javascript 中不断添加数字?
我能够计算出用户输入的内容,并将其汇总。totaal1但是,totaal2在这种情况下,如何计算 Javascript 在 div id 中打印的内容?

<HTML>
    <HEAD>
        <TITLE>Hier vul je de titel van je pagina in.</TITLE>
    </HEAD>
    <BODY>

    <script type="text/javascript">
    function test() {
        var getal1 = ( document.javascript.normaal1.value - 0 );
        var getal2 = ( document.javascript.alu1.value - 0 );
        var getal3 = ( document.javascript.rvs1.value - 0 );

        var getal4 = ( document.javascript.normaal2.value - 0 );
        var getal5 = ( document.javascript.alu2.value - 0 );
        var getal6 = ( document.javascript.rvs2.value - 0 );

        var getal7 = 'totaal1' + 'totaal2';

        document.getElementById('totaal1').innerHTML = (getal1*0.40) + (getal2*0.50) + (getal3*0.60);

        document.getElementById('totaal2').innerHTML = (getal4*0.40) + (getal5*0.50) + (getal6*0.60);

        document.getElementById('totaal3').innerHTML = getal7;
    }
    </script>

    <form method='post' name='javascript'>

    <table border="1">
      <tr>
        <th>Ring</th>
        <th>Normaal Per stuk 0,40 </th>
        <th>Aluminium Per stuk 0,50 </th>
        <th>RVS Per stuk 0,60 </th>
        <th>Totaal</th>
      </tr>
      <tr>
        <td>1mm</td>
        <td><input type='text' name='normaal1' onchange='test()' ></td>
        <td><input type='text' name='alu1' onchange='test()' ></td>
        <td><input type='text' name='rvs1' onchange='test()' ></td>
        <td><div id="totaal1">&nbsp;</div></td>
      </tr>
      <tr>
        <td>2mm</td>
        <td><input type='text' name='normaal2' onchange='test()' ></td>
        <td><input type='text' name='alu2' onchange='test()' ></td>
        <td><input type='text' name='rvs2' onchange='test()' ></td>
        <td><div id="totaal2">&nbsp;</div></td>
      </tr>
      <tr>
        <td>Totaal</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td><div id="totaal3">&nbsp;</div></td>
      </tr>
    </form>

    </table>   
    </BODY>
</HTML>
4

1 回答 1

0

尝试

var getal7 = parseFloat(document.getElementById('totaal1').textContent) +
  parseFloat(document.getElementById('totaal2').textContent);
于 2013-11-07T20:56:05.153 回答