简体   繁体   中英

Javascript: Get all form input values as an array

I am trying to have the NumberBox's onEnter() function to be able to dynamically change the formBox.

If someone types 3 as the input value, I need it to show 3 forms with the exact input boxes. I know how to get the value from the input box and show the amount of forms below.

My problem is that I cannot figure out how to have one submit button store all of the values into an array, such as [[1,1,1,1,1],[2,2,2,2,2],etc].

Once I can figure this out, I should be able to output those values on my own. End goal is to use those values to show the list multiple times on another page. I would prefer to keep this completely in Javascript. I believe it can be done, but I have hit a block and need some help.

 function clickMe() { var input1 = document.forms["formBox"]["info1"].value; var input2 = document.forms["formBox"]["info2"].value; var input3 = document.forms["formBox"]["info3"].value; var input4 = document.forms["formBox"]["info4"].value; var input5 = document.forms["formBox"]["info5"].value; var inputArr = [input1,input2,input3,input4,input5]; document.getElementById("info1").innerHTML = inputArr[0]; document.getElementById("info2").innerHTML = inputArr[1]; document.getElementById("info3").innerHTML = inputArr[2]; document.getElementById("info4").innerHTML = inputArr[3]; document.getElementById("info5").innerHTML = inputArr[4]; console.log(inputArr); } function onEnter() { var qNumber = document.getElementsByName("numberBox")[0].value; if(event.keyCode == 13) { var qID = document.getElementById("numBox"); var submitBtn = document.getElementById("submitButton"); var a = qNumber - 1; var b = 0; while (b < a) { var formClone = document.getElementById("formBox"); var listClone = formClone.cloneNode(true); var text =b+2; listClone.id = "formBox" + text; console.log(listClone.id); document.getElementById("forms").append(listClone); b++; console.log(b); } return qID.parentNode.removeChild(qID); } return qNumber; }
 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: </label> <input type="text" name="info3" /> <label for="info4">Input 4: </label> <input type="text" name="info4" /> <label for="info5">Input 5: </label> <input type="text" name="info5" /> </form> </div> <input type="submit" value="Submit" id="submitButton" onclick="clickMe()" /> <div id="content"> <span id="info1">input1</span> <br/> <span id="info2">input2</span> <br/> <span id="info3">input3</span> <br/> <span id="info4">input4</span> <br/> <span id="info5">input5</span> </div>

You can query all input elements within form, map their values to another array.

[...document.forms["formBox"].getElementsByTagName("input")].map(input => input.value)

I was able to loop through all of the inputs, thanks @mike-ezzati and @metamorph_online for pointing me in the right direction. This is my way of doing so, and I was realizing that I needed to bring over the value from the onEnter() function. I ended up using localStorage in order to accomplish this easiest.

    function clickMe() {
        var q = localStorage.getItem("qNumber");
        console.log(q);
        var inputNow = [];
        var allInputs = [];
        var inputNow = document.getElementsByTagName("input");
        for(x=0; x < inputNow.length; x++) {
            allInputs.push(inputNow[x].value);
             console.log(allInputs);
        }
    localStorage.clear();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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