简体   繁体   English

如何保持更新本地存储JavaScript中的数据

[英]How to keep updating data in local storage JavaScript

I am getting a value from a text field and store it in local storage because I need to show the same value typed by the user back to the text field after some validation. 我正在从文本字段中获取一个值并将其存储在本地存储中,因为在进行某些验证后,我需要将用户键入的相同值显示回文本字段。 Once the value is stored it doesn't update itself because every time I change the value in the text field I expect to store the same value in the local storage but it only does it one and displays the same value every time it stored the first time. 一旦存储了值,它不会自动更新,因为每次我在文本字段中更改值时,我都希望将相同的值存储在本地存储中,但是它只会执行一次,并且在每次存储第一个时都显示相同的值时间。

jsp JSP

<s:textfield name="emailId" id="emailId" label="Email" cssClass="dataFieldCell3" value="%{#signerslist.email}"  />

JS JS

setting the variable in local storage 在本地存储中设置变量

function showEmail() {
    var email = document.getElementById("emailId").value;
    localStorage.setItem("invalidEmail", email);
} 

getting it and assign it back in the text field 获取并将其分配回文本字段

function emailValidationErrorMessage() {
    if('${emailValidationMessage}' !== "") {
        $("#Signers").show();
        var getEmailValue = localStorage.getItem("invalidEmail");
        document.getElementById("emailId").value = getEmailValue;
    }
}

Please guide... 请指导...

Thanks :) 谢谢 :)

...every time I change the value in the text field I expect to store the same value in the local storage... ...每次我更改文本字段中的值时,都希望将相同的值存储在本地存储中...

Then, the showEmail function needs to be set as the callback function for the text field's change event so that localStorage will be updated each time the user changes the value in the text field. 然后,需要将showEmail函数设置为文本字段的change事件的回调函数,以便每次用户更改文本字段中的值时都会更新localStorage

document.getElementById("emailId").addEventListener("change", showEmail);

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

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