简体   繁体   English

将输入文本保存到本地存储时遇到问题

[英]Having trouble saving input text to local storage

I'm making a planner application that allows inputted text to be saved to local storage by the hour and I cannot figure out how to fix my javascript to make it work.我正在制作一个计划程序应用程序,允许按小时将输入的文本保存到本地存储,但我不知道如何修复我的 javascript 以使其工作。 HTML: HTML:

          <tr class="row" id="17">
            <th scope="time" id="hour17" class="time">17:00</th>
            <td><input type="text" class="textbox" id="h17input" onclick = "savePlan()"></td>
            <td class="btnContainer">
              <button class="saveBtn"><i class="fas fa-save"></i></button>
            </td>
          </tr>

JS: JS:

var input = document.getElementById('input');

function savePlan() {
    localStorage.setItem(input.innerText);
    console.log(input);
    localStorage.getItem(input.innerText)
};

Thank you!谢谢!

need 2 parameters : localStorage.setItem(keyName, keyValue);需要 2 个参数: localStorage.setItem(keyName, keyValue); . . You are missing keyName您缺少keyName

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

localStorage.getItem requires a keyName to store the value under a name. localStorage.getItem需要一个keyName来将值存储在一个名称下。 And you need to use the value property of the input instead of the innerText .并且您需要使用inputvalue属性而不是innerText

const button = document.querySelector('.saveBtn');
const input = document.getElementById('h17input');

button.addEventListener('click', savePlan);

function savePlan() {
  localStorage.setItem('plan', input.value);
}

function getPlan() {
  return localStorage.getItem('plan');
}

You can think that localstorage works "like" an object, with key and values.你可以认为localstorage像一个对象一样工作,有键和值。 If you want to store something there, you need to enter a key ( keyName ) and a value (your input ).如果你想在那里存储一些东西,你需要输入一个键( keyName )和一个值(你的input )。 Now, if you want to get a value from there, you just need the key.现在,如果您想从那里获取值,您只需要密钥。

https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

So, in your case:所以,在你的情况下:

var input = document.getElementById('input');

function savePlan() {
    // store input.value
    localStorage.setItem('myCustomStorage:month', input.value); 
    
    // return input.value on localStorage
    return localStorage.getItem('myCustomStorage:month') 
};

I have made something like this:我做了这样的事情:

*Update: I added an id ( saveBtn ) to the button. *更新:我在按钮上添加了一个 id ( saveBtn )。

let textbox = document.getElementById('h17input');
let saveBtn = document.getElementById('saveBtn');
saveBtn.addEventListener('click',savePlan);
function savePlan() {
    //localStorage.setItem(input.innerText);
    window.localStorage.setItem("saved_data", JSON.stringify(textbox.value))
    //localStorage.getItem(input.innerText)
    let  savedItem = JSON.parse(localStorage.getItem("saved_data"));
    console.log(savedItem);
};

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

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