数组总和返回 NaN

我有一个包含三个值和 1 个单独值的数组,所有值均从表单获取。


我正在尝试将顶部数字 (3) 与其他数字(12、3 和 31)相乘,然后将它们相加。


这是我尝试过的:


HTML:


 <p>Aantal sets: <span id="dynamicSet"></span></p>

        <table id="resultTable" cellpadding="1" cellspacing="1" border="1">

            <tr>

                <th scope="col"></th>

                <th scope="col">Hoeveelheid</th>

                <th scope="col">Gewicht x KG</th>

            </tr>

            <tr>

                <td>1</td>

                <td class="HoeveelheidField"><INPUT TYPE="NUMBER" MIN="0" MAX="10" STEP="1" ></td>

                <td class="GewichtField"><INPUT TYPE="NUMBER" MIN="0" MAX="10" STEP="1" ></td>

            </tr>

            <tr>

                <td>2</td>

                <td class="HoeveelheidField"><INPUT TYPE="NUMBER" MIN="0" MAX="10" STEP="1" ></td>

                <td class="GewichtField"><INPUT TYPE="NUMBER" MIN="0" MAX="10" STEP="1" ></td>

            </tr>

            <tr>

                <td>3</td>

                <td class="HoeveelheidField"><INPUT TYPE="NUMBER" MIN="0" MAX="10" STEP="1" ></td>

                <td class="GewichtField"><INPUT TYPE="NUMBER" MIN="0" MAX="10" STEP="1" ></td>

            </tr>

        </table>

还有我的JS:


var HoeveelheidArr=[];

var total = 0;

const setAmount = document.getElementById("dynamicSet").innerHTML;


function getData(){

    $('#resultTable .HoeveelheidField > input ').each(function() {

        HoeveelheidArr.push($(this).val());

    });


    console.log(HoeveelheidArr);


       HoeveelheidArr.forEach(function getReps (value) {

           console.log(value);

           for(var i = 0; i < HoeveelheidArr.length; i++){

               total += value[i] * setAmount;

           }


       });

    console.log(total);

}

然而,正如您在图片中看到的那样,我不断从控制台返回&ldquo;不是数字&rdquo;。尽管控制台显示它们全部显示为整数?你看到我做错了什么了吗?我也尝试过:


parseInt(value[i]) 


互换的青春
浏览 198回答 2
2回答

烙印99

您的setAmount变量是来自 的字符串innerHTML。对两个变量都使用parseInt():total&nbsp;+=&nbsp;parseInt(value[i])&nbsp;*&nbsp;parseInt(setAmount);更新经过更深入的审查后,您发现了一些其他问题。您的setAmount变量是"",因为它没有在 HTML 中设置。你正在循环HoeveelheidArr两次你应该使用value而不是value[i]我做了一个工作示例:var HoeveelheidArr = [];var total = 0;const setAmount = document.getElementById("dynamicSet").innerHTML || 0;function getData() {&nbsp; HoeveelheidArr = [];&nbsp; total = 0;&nbsp; $('#resultTable .HoeveelheidField > input').each(function() {&nbsp; &nbsp; HoeveelheidArr.push($(this).val() || 0);&nbsp; });&nbsp; console.log("HoeveelheidArr:", HoeveelheidArr);&nbsp; for (var i = 0; i < HoeveelheidArr.length; i++) {&nbsp; &nbsp; console.log("setAmount:", setAmount);&nbsp; &nbsp; total += parseInt(HoeveelheidArr[i]) * parseInt(setAmount);&nbsp; }&nbsp; console.log("Total:", total);}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><p>Aantal sets: <span id="dynamicSet">1</span></p><table id="resultTable" cellpadding="1" cellspacing="1" border="1">&nbsp; <tr>&nbsp; &nbsp; <th scope="col"></th>&nbsp; &nbsp; <th scope="col">Hoeveelheid</th>&nbsp; &nbsp; <th scope="col">Gewicht x KG</th>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>1</td>&nbsp; &nbsp; <td class="HoeveelheidField">&nbsp; &nbsp; &nbsp; <INPUT TYPE="NUMBER" MIN="0" MAX="10" STEP="1">&nbsp; &nbsp; </td>&nbsp; &nbsp; <td class="GewichtField">&nbsp; &nbsp; &nbsp; <INPUT TYPE="NUMBER" MIN="0" MAX="10" STEP="1">&nbsp; &nbsp; </td>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>2</td>&nbsp; &nbsp; <td class="HoeveelheidField">&nbsp; &nbsp; &nbsp; <INPUT TYPE="NUMBER" MIN="0" MAX="10" STEP="1">&nbsp; &nbsp; </td>&nbsp; &nbsp; <td class="GewichtField">&nbsp; &nbsp; &nbsp; <INPUT TYPE="NUMBER" MIN="0" MAX="10" STEP="1">&nbsp; &nbsp; </td>&nbsp; </tr>&nbsp; <tr>&nbsp; &nbsp; <td>3</td>&nbsp; &nbsp; <td class="HoeveelheidField">&nbsp; &nbsp; &nbsp; <INPUT TYPE="NUMBER" MIN="0" MAX="10" STEP="1">&nbsp; &nbsp; </td>&nbsp; &nbsp; <td class="GewichtField">&nbsp; &nbsp; &nbsp; <INPUT TYPE="NUMBER" MIN="0" MAX="10" STEP="1">&nbsp; &nbsp; </td>&nbsp; </tr></table><button onclick="getData()">Test</button>

小唯快跑啊

为什么你使用 value[i] value 不是一个数组,它只是一个数字。console.log([value[i]);value[1] 和 value[2] 未定义,这就是为什么你得到 NaN如果你告诉我你想通过这段代码做什么,我可以纠正它
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5