我对数组的数组有问题。我需要该函数clickMe()来允许我输出一个数组,例如 [[1,1,1,1,1],[2,2,2,2,2] 等]。我的问题是现在这些值显示为 [1,1,1,1,1,2,2,2,2,2,etc]。我知道 for 循环内的 for 循环将是最好的方法,但是我将如何获得五个部分的输入?一旦我能弄清楚这一点,我应该能够从这些数组中提取而不会出现任何问题。我更愿意将其完全保留在 Javascript 中。 var qNumber; function onEnter() { var qNumber = document.getElementsByName("numberBox")[0].value; if(event.keyCode == 13) { if (typeof(Storage) !== "undefined") { localStorage.setItem("qNumber", qNumber); console.log(qNumber + " stored successfully"); } else { console.log("Sorry, your browser does not support Web Storage..."); } var qID = document.getElementById("numBox"); var submitBtn = document.getElementById("submitButton"); var a = qNumber - 1; var b = 0; while (b < a) { var formClone = document.getElementsByClassName("formBox")[0]; var listClone = formClone.cloneNode(true); var text =b+2; document.getElementById("forms").append(listClone); b++; } return qID.parentNode.removeChild(qID); } return qNumber;}function clickMe() { var q = localStorage.getItem("qNumber"); console.log(q); var inputNow = []; var allInputs = []; var eachArray = []; var inputNow = document.getElementsByTagName("input"); for(x=0; x < inputNow.length; x++) { allInputs.push(inputNow[x].value); console.log(allInputs); } localStorage.clear();}input{ display: block;}<div id="forms"> <span id="numBox"> <label for="numberBox">Number of Forms</label> <input type="number" name="numberBox" onkeydown="onEnter()" /> </span> <form id="formBox" name="formBox" action="#" onsubmit="return false;"> <label for="info1">Input 1:</label> <input type="text" name="info1" /> <label for="info2">Input 2: </label> <input type="text" name="info2" /> <label for="info3">Input 3:
1 回答
RISEBY
TA贡献1856条经验 获得超5个赞
你总是可以做这样的事情:
var allInputs = [];
var groupInputs = [];
for (x=0; x < inputNow.length; x++) {
groupInputs.push(inputNow[x].value);
if (groupInputs.length === 5 || x === inputNow.length - 1) {
allInputs.push(groupInputs);
groupInputs = [];
}
}
添加回答
举报
0/150
提交
取消