简体   繁体   English

循环输入并添加它们

[英]Loop through inputs and add them

Trying to loop through all the inputs, when more are added, and add them all to the total (var = paidTotal). 当添加更多输入时,尝试遍历所有输入,并将它们全部添加到总计中(var = paidTotal)。 I'm getting NaN for the total and Cannot read property 'length' of undefined. 我得到的总数是NaN,无法读取未定义的属性“ length”。 Pretty sure the issue is in the peoplepaid() function. 可以确定问题出在peoplepaid()函数中。

In the peoplePaid() onclick function I'm trying to loop through all the fieldInputs (class="person") depending on how many inputs are created and add the total to #paidTotal. 在peoplePaid()onclick函数中,我试图遍历所有fieldInputs(class =“ person”),具体取决于创建了多少输入并将总和添加到#paidTotal中。 Hopefully that's helpful. 希望这会有所帮助。

 $(document).ready(function() { var maxFields = 20; var addButton = $('#plusOne'); var deleteButton = $('#minusOne'); var wrapper = $('#userNumbers'); var fieldInput = '<div><input type="text" name="persons" class="persons"/></div>'; var x = 1; $(addButton).click(function() { if (x < maxFields) { x++; $(wrapper).append(fieldInput); } }); $(deleteButton).click(function(e) { e.preventDefault(); var myNode = document.getElementById("userNumbers"); i = myNode.childNodes.length - 1; if (i >= 0) { myNode.removeChild(myNode.childNodes[i]); x--; } }); }); function peoplePaid() { var checkTotal = document.getElementById('check').value; var personsCheck = document.getElementsByClassName('persons').value; var inputs = document.getElementsByClassName('persons'); var paidTotal = document.getElementById('paidTotal'); for (var i = 1; i < personsCheck.length; i += 1) { paidTotal[i] += personsCheck; } paidTotal.innerHTML = checkTotal - personsCheck; } 
 <h3>Check Total</h3> $ <input type="text" id="check" value="" /> <h3>Number of People: <span id="numberOfPeople"></span></h3> <button type="button" onclick="plusOne()" id="plusOne">+</button> <button type="button" onclick="minusOne()" id="minusOne">-</button> <div> <div id="userNumbers"> <input type="text" class="persons" name="person"> </div> </div> <button onclick="peoplePaid()">Calculate</button> <!--Paid Amount--> <div> <h3>Paid Amount: <span id="paidTotal"></span></h3> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

The reason you are getting Cannot read property 'length' of undefined is because 您得到无法读取未定义的属性“长度”的原因是因为

var personsCheck = document.getElementsByClassName('persons').value;

will return undefined. 将返回未定义。

document.getElementsByClassName('persons')

will return an array of DOM elements and arrays do not have a value property. 将返回DOM元素数组,并且该数组没有value属性。 Then when you access 然后,当您访问

personsCheck.length

in the loop you get Cannot read property 'length' of undefined because personsCheck is undefined. 在循环中,您得到“无法读取未定义的属性” length”,因为personsCheck是未定义的。

I refactored a bit but I used map to get an array of the values then summed them up using reduce 我进行了一些重构,但是我使用map获取了一个值数组,然后使用reduce对其求和

run the snippet below 运行下面的代码段

 $(document).ready(function() { const maxFields = 20; const wrapper = $('#userNumbers'); const fieldInput = '<div><input type="text" name="persons" class="persons"/></div>'; const reducer = (a,c) => parseInt(a,10) + parseInt(c,10); $('#plusOne').click(() => $("#userNumbers .persons").length < maxFields && $(wrapper).append(fieldInput)); $('#deleteButton').click(()=> $("#userNumbers .persons").last().remove()); $('#peoplePaid').click( () => { const arr= $("#userNumbers .persons").map(function(){ return $(this).val(); }).get(); $('#paidTotal').html(arr.reduce(reducer)); }) }); 
 <h3>Check Total</h3> $ <input type="text" id="check" value="" /> <h3>Number of People: <span id="numberOfPeople"></span></h3> <button type="button" id="plusOne">+</button> <button type="button" id="minusOne">-</button> <div> <div id="userNumbers"> <input type="number" class="persons" name="person"> </div> </div> <button id="peoplePaid">Calculate</button> <!--Paid Amount--> <div> <h3>Paid Amount: <span id="paidTotal"></span></h3> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

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

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