简体   繁体   English

如何计算数字输入的值?

[英]How can I count the values of my number inputs?

I have an form and I want the completed numbers to be added together.我有一个表格,我希望将完整的数字加在一起。 Nothing has to be done with the text and date input.文本和日期输入无需执行任何操作。 I want to display the total of numbers in the span element with id "total".我想显示 id 为“total”的 span 元素中的数字总数。

 function myFunction() { var x = document.getElementById("form"); total = 0; var i; for (i = 0; i < x.length ;i++) { total = total + x.elements[i].value; } document.getElementById("demo").innerHTML = total; }
 <form id="form"> <tr> <td><input type="text"></td> <td><input type="number"></td> <td><input type="date"></td> </tr> <tr> <td><input type="text"></td> <td><input type="number"></td> <td><input type="date"></td> </tr> <tr> <td><input type="text"></td> <td><input type="number"></td> <td><input type="date"></td> </tr> </form> </table> </div> <button class="trigger" onclick="myFunction()"> Bereken studietempo </button> <div class="modal"> <div class="modal-content"> <span class="close-button">&times;</span> <p>Je hebt tot nu toe <span id="total"></span> ECTS behaald.</p> <p>Je huidige studietempo is:</p> <p>0 ECTS / maand.</p> <p>Op basis van dit studietempo heb je 15 maanden nodig voor je P</p> </div> </div>

Thankyou!谢谢!

I want to display the total of numbers in the span element with id "total".我想显示 id 为“total”的 span 元素中的数字总数。

You are trying to add up all the values as Elements x.elements doesn't exclude text and date ,您正在尝试将所有值相加,因为Elements x.elements不排除textdate

Use querySelector and reduce使用querySelectorreduce

var x = document.getElementById("form");
var total = Array.from( x.querySelectorAll( "[type='number']" ) )
                 .reduce( ( a, c ) => a + ( isNaN( c ) ? 0 + c ), 0 );

I want to display the total of numbers in the span element with id "total".我想显示 id 为“total”的 span 元素中的数字总数。

Now display this total现在显示这个总数

document.getElementById("total").innerHTML = total;

You need to loop using this for-loop :您需要使用此for-loop

for (var i = 0; i < x.elements.length; i++) {...}

Parse the values as follow:解析值如下:

total = total + (isNaN(Number(x.elements[i].value)) ? 0 : Number(x.elements[i].value));

The first input in every TD is type of text , change to number每个 TD 中的第一个输入是text类型,更改为number

<td><input type="text"></td>
                 ^

 function myFunction() { var x = document.getElementById("form"); var total = 0; for (var i = 0; i < x.elements.length; i++) { if (x.elements[i].type === 'number') { total += (isNaN(Number(x.elements[i].value)) ? 0 : Number(x.elements[i].value)); } } document.getElementById("demo").innerHTML = total; }
 <table> <form id="form"> <tr> <td><input type="number"></td> <td><input type="number"></td> <td><input type="date"></td> </tr> <tr> <td><input type="number"></td> <td><input type="number"></td> <td><input type="date"></td> </tr> <tr> <td><input type="number"></td> <td><input type="number"></td> <td><input type="date"></td> </tr> </form> </table> <button class="trigger" onclick="myFunction()"> Bereken studietempo </button> <div class="modal"> <div class="modal-content"> <!--span class="close-button">&times;</span> <p>Je hebt tot nu toe <span id="total"></span> ECTS behaald.</p> <p>Je huidige studietempo is:</p> <p>0 ECTS / maand.</p> <p>Op basis van dit studietempo heb je 15 maanden nodig voor je P</p--> </div> </div> <p>Demo</p> <span id="demo"></span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM