简体   繁体   English

为什么我不能让它显示

[英]why am I not able to get this to display

I have this reminder app which allows you to have a name for it, a date and some extra information.我有这个提醒应用程序,它允许您为其命名、日期和一些额外信息。 I am able to get it to store locally but I can't get it to display.我可以将其存储在本地,但无法显示。 Can I get some help to get it to display?我可以得到一些帮助让它显示吗?

 let reminders = []; const addReminders = (ev) => { ev.preventDefault(); let reminder = { ReminderInput: document.getElementById("ReminderInput").value, DateInput: document.getElementById("DateInput").value, InfoInput: document.getElementById("InfoInput").value, }; const arr = [reminder.ReminderInput, reminder.DateInput, reminder.InfoInput]; localStorage.setItem("todoForm", JSON.stringify(arr)); reminders.push([ reminder.ReminderInput, reminder.DateInput, reminder.InfoInput, ]); localStorage.setItem("reminders", JSON.stringify(reminders)); localStorage.getItem("reminders", JSON.parse(reminders)); }; document.addEventListener("DOMContentLoaded", () => { document.getElementById("btn").addEventListener("click", addReminders); });
 <form id="todoForm"> <label for="ReminderInput">Reminder</label> <input class="u-full-width" type="text" id="ReminderInput"> <label for="DateInput">Date</label> <input class="u-full-width" type="datetime-local" id="DateInput"> <label for="InfoInput">Additional Information</label> <textarea class="u-full-width" type="text" placeholder="Remember to..." id="InfoInput"></textarea> <button type="button" id="btn" class="button-primary">Add Reminder</button> </form>

change this line localStorage.getItem("reminders", JSON.parse(reminders));更改此行localStorage.getItem("reminders", JSON.parse(reminders)); with var reminderData=JSON.parse(localStorage.getItem("reminders"));使用var reminderData=JSON.parse(localStorage.getItem("reminders"));

localStorage.getItem("reminders") is the correct way to fetch localstorage data localStorage.getItem("reminders")是获取本地存储数据的正确方法

`     
    let reminders = [];    
    const addReminders = (ev) => {
      ev.preventDefault();
      let reminder = {
        ReminderInput: document.getElementById("ReminderInput").value,
        DateInput: document.getElementById("DateInput").value,
        InfoInput: document.getElementById("InfoInput").value,
      };

      const arr = [reminder.ReminderInput, reminder.DateInput, 
 reminder.InfoInput];

      localStorage.setItem("todoForm", JSON.stringify(arr));
      reminders.push([
        reminder.ReminderInput,
        reminder.DateInput,
        reminder.InfoInput,
      ]);
      localStorage.setItem("reminders", JSON.stringify(reminders));
      display();
    };

    document.addEventListener("DOMContentLoaded", () => {  
      document.getElementById("btn").addEventListener("click", addReminders);

    });

    function display()
    {
        var reminderData =JSON.parse(localStorage.getItem("reminders"));
        alert(reminderData);
        for(let i = 0; i < reminderData.length;i++)
        {
            alert(reminderData[i][0]);
            alert(reminderData[i][1]);
            alert(reminderData[i][2]);
        }
    }
` 

//you can use the getItem operation.

//you would have to 
//1. get item
//2. parse it (from string to array)
//3. iterate over and display

//above is the javascript. change date-time to date...then you can add in 
//the values however you like to an empty div. call display when the browser 
//loads as well

If you want to display your data firstly you have to mention where you want display your data.如果要首先显示数据,则必须提及要显示数据的位置。 if you want to just test that value is coming so just use this:如果您只想测试该值即将到来,请使用以下命令:

var data = localStorage.getItem("reminders");
alert(data);    

OR或者

console.log(data);

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

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