如何在Javascript中添加数字?

时间:2013-11-07 20:50:43

标签: javascript

如何在Javascript中添加数字? 我能够计算用户输入的内容,并总计这一点。但是如何在div id中创建Javascript打印的内容,例如在totaal1和totaal2中?

<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>

1 个答案:

答案 0 :(得分:0)

尝试

var getal7 = parseFloat(document.getElementById('totaal1').textContent) +
  parseFloat(document.getElementById('totaal2').textContent);