簡體   English   中英

盡管刷新頁面仍保存輸入值

[英]save input value despite the page being refreshed

我是一名新程序員,對學習JavaScript非常感興趣。 我正在輸入用戶在其中輸入金額的輸入框。 我希望保存它,直到用戶nextTime編輯/更新它為止,但是我正在努力做到這一點。 我一直在互聯網上尋找它一個月之久,但找不到很多我能理解的東西。 我知道使用Cookie的方法,但是我真的不知道如何編寫Cookie,我只需要一些提示和幫助即可。 謝謝。

的HTML

<form id="update" onsubmit="return false" method="post">
    <td><input class="rateContainer" value="Rate" disabled></td>
    <td><input type="number" id="rate"></input></td>
    <td><input type="submit" value ="Update" onclick="Update();" /></td>
</form>

JAVASCRIPT

function Update() {
    var Update = document.getElementById("rate").submit;
}

不要使用cookie。 使用localStoragehttps : //developer.mozilla.org/en/docs/Web/API/Window/localStorage

查看localStorage:

window.onload=function(){
var input=document.getElementById("yourinputid");
if(localStorage.getItem("input")){
//if there is a stored value, apply it to the input
input.value=localStorage.getItem("input");
}
input.oninput=function(){
//if input is changed, store it
  localStorage.setItem("input",input.value);
};
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM