刷新页面后保留输入值

刷新页面后保留输入值,第1张

刷新页面后保留输入

编辑:保留更多输入的价值

HTML:

<input type="text" id="txt_1" onkeyup='savevalue(this);'/> <input type="text" id="txt_2" onkeyup='savevalue(this);'/>

Javascript:

<script type="text/javascript">        document.getElementById("txt_1").value = getSavedValue("txt_1");    // set the value to this input        document.getElementById("txt_2").value = getSavedValue("txt_2");   // set the value to this input                //Save the value function - save it to localStorage as (ID, VALUE)        function savevalue(e){ var id = e.id;  // get the sender's id to save it .  var val = e.value; // get the value.  localStorage.setItem(id, val);// Every time user writing something, the localStorage's value will override .         }        //get the saved value function - return the value of "v" from localStorage.         function getSavedValue  (v){ if (!localStorage.getItem(v)) {     return "";// You can change this to your defualt value.  } return localStorage.getItem(v);        }</script>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5621504.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存