简体   繁体   English

如何从同一个输入框中获取多个输入并存储它们?

[英]How Do I Take Multiple Inputs From the same Input Box and store them?

I am currently taking inputs about a new event and storing them in local storage like this.我目前正在接受有关新事件的输入并将它们存储在这样的本地存储中。

<form name="myform" action="" method="GET">
   Event Name: <INPUT TYPE="text" NAME="name" VALUE="" id="input1"><br />
   Event Date and Time: <INPUT TYPE="datetime-local" NAME="date" Value="" id="input2"><br />
   Event Location: <INPUT TYPE="text" NAME="location" VALUE="" id="input3"><br />
   Event Notes: <INPUT TYPE="text" NAME="notes" VALUE="" id="input4"><br />
   <button onclick="storeValues()" type=submit>Submit</button>
</form>

    <script>
   document.getElementById('input1').value = localStorage.getItem("EventName");
   document.getElementById('input2').value = localStorage.getItem("EventDateAndTime");
   document.getElementById('input3').value = localStorage.getItem("EventLocation");
   document.getElementById('input4').value = localStorage.getItem("EventNotes");


function storeValues() {
    localStorage.setItem("EventName", document.getElementById('input1').value);
    localStorage.setItem("EventDateAndTime", document.getElementById('input2').value);
    localStorage.setItem("EventLocation", document.getElementById('input3').value);
    localStorage.setItem("EventNotes", document.getElementById('input4').value);
}
</script>

But I am pretty sure whenever I take a second input it overwrites it in local storage.但我很确定,每当我进行第二次输入时,它都会在本地存储中覆盖它。 How would I change it so i can take these inputs multiple times?我将如何更改它以便我可以多次获取这些输入?

If you want them to both be stored, then you have to name them differently.如果您希望它们都被存储,那么您必须以不同的方式命名它们。

What you probably want is something like an array of values in local storage, which if I remember correctly doesn't work out of the box?您可能想要的是本地存储中的值数组,如果我没记错的话,它不能开箱即用? I don't really remember, but you could probably serialize/unserialize this array data as a JSON string.我真的不记得了,但是您可能可以将此数组数据序列化/反序列化为 JSON 字符串。

you will need to store an array in storage (of course you will have to stringify the array to store as string and later parse it to operate onto it.)您将需要在存储中存储一个数组(当然,您必须将数组字符串化以存储为字符串,然后对其进行解析以对其进行操作。)

I have written a snippet it will give you an idea.我写了一个片段,它会给你一个想法。

<form name="myform" action="" method="GET">
    Event Name: <INPUT TYPE="text" NAME="name" VALUE="" id="input1"><br />
    Event Date and Time: <INPUT TYPE="datetime-local" NAME="date" Value="" id="input2"><br />
    Event Location: <INPUT TYPE="text" NAME="location" VALUE="" id="input3"><br />
    Event Notes: <INPUT TYPE="text" NAME="notes" VALUE="" id="input4"><br />
    <button onclick="storeValues()" type=button>Submit</button>

    <ul id="eventLists">

    </ul>
</form>

<script>

    function loadEvents() {
        let ul = document.getElementById("eventLists");
        ul.innerHTML = "";
        let events = localStorage.getItem("events");
        if (events) {
            try {
                events = JSON.parse(events);
                events.map(e => {
                    ul.innerHTML += `<li>Event named as '${e.name}' dated on '${e.date}' will be held at ${e.location}. (${e.notes})</li>`
                })
            } catch (err) {
                console.log(err);
            }
        }
    }


    loadEvents();

    function storeValues() {
        // getting previous values
        let events = localStorage.getItem("events");


        let newEvent = {
            name: document.getElementById('input1').value,
            date: document.getElementById('input2').value,
            location: document.getElementById('input3').value,
            notes: document.getElementById('input4').value
        }

        try {
            if (events) {
                events = JSON.parse(events);
                events.push(newEvent);
            } else {
                throw Error("no events key here."); // just to take us to the catch block
            }
        } catch (err) {
            events = [newEvent];
        } finally {
            // save into storage
            localStorage.setItem("events", JSON.stringify(events));
            loadEvents();
        }
    }
</script>

I think you are trying to reuse this form to create multiple events and store those data to your localStorage.我认为您正在尝试重用此表单来创建多个事件并将这些数据存储到您的 localStorage。 If that's the case then you can check my solution.如果是这种情况,那么您可以查看我的解决方案。

 <:DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <form name="myform" action="" method="GET"> Event Name: <INPUT TYPE="text" NAME="name" VALUE="" id="input1"><br /> Event Date and Time: <INPUT TYPE="datetime-local" NAME="date" Value="" id="input2"><br /> Event Location: <INPUT TYPE="text" NAME="location" VALUE="" id="input3"><br /> Event Notes. <INPUT TYPE="text" NAME="notes" VALUE="" id="input4"><br /> <button onclick="storeValues(event)" type=submit>Submit</button> </form> <script> function storeValues(e) { e;preventDefault(). let storedEvents = JSON.parse(localStorage;getItem("Events")) || []: const newEventDetails = { name. document.getElementById('input1'),value: dateTime. document.getElementById('input2'),value: location. document.getElementById('input3'),value: notes. document.getElementById('input4').value } storedEvents;push(newEventDetails). localStorage,setItem("Events". JSON;stringify(storedEvents)). console,log('storedEvents'; storedEvents); } </script> </body> </html>

暂无
暂无

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

相关问题 如何从输入中获取多个字符串并将它们放入同一数组中? - How to take multiple strings from inputs and place them into the same array? 我想从多个文本输入中获取输入并将其发送到我的数据库,但是只有最后一个输入发送 - I want to take input from multiple text inputs and send them to my database, but only the last input sends 如何使用隐藏的输入框进行输入? - How can I take inputs with hidden input box? 如何从文本字段中获取用户输入,并在Google脚本中将其设置为变量? - How do I take user inputs from a textfield and make them variables in a Google Script? 我如何使用 JavaScript 将输入从文本框中获取到变量然后打印变量的值? - How do i use JavaScript to take the input from a text box to a variable then print the value of a variable? 如何从表单获取输入/输出并将其存储在JS变量中? - How do I take input/output from a form and store it in a JS variable? 如何从 javascript 中的控制台获取多个输入? - How to take multiple inputs from console in javascript? 将多个值从单个输入框存储到本地存储并提交按钮并将它们返回给其他按钮 - Store multiple values to local storage from a single input box and submit button and return them to feed other buttons 如何从搜索栏获取输入并使用Javascript和HTML同时处理搜索列表? - How do I take an input from a search bar and handle a search list at the same time with Javascript and HTML? 如何制作表格(两个输入)将我带到另一页,其选择取决于输入 - How do I make form (two inputs) take me to another page which selections depends on the input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM