繁体   English   中英

单击提交按钮后如何保留文本框值

[英]How to retain textbox value after click submit button

单击提交按钮文本框值后,我有一个搜索文本框,但当我重新加载页面时,文本框值仍在文本框中,如何在重新加载/刷新页面后删除文本框的值。

<script>
    $(document).ready(function(){
    document.getElementById("text1").value = localStorage.getItem("item1");
        });

   $(window).on('beforeunload', function() {
    localStorage.setItem("item1",document.getElementById("text1").value);
  });
</script>

html。

<td>
<form action="/searchemp" method="post" >
    Selected Employee <input type="text"  name="EmployeeName" id="text1">
                     <input type ="submit" value="check">
</form> 

给我一个帮助或建议来解决它。

谢谢你。

        $(document).ready(function () {
            $("#form1").submit(function () {
                window.localStorage['text1_val'] = $("input[name = 'text1']").val();
            });
            $(window).load(function () {
                $("input[name = 'text1']").val(window.localStorage['text1_val']);
                window.localStorage['text1_val'] = '';
            });
        });

  <form id="form1" method="post">
    <input type="text" name="text1">
    <input type="submit">
  </form>

默认情况下,页面加载时文本框值为空。 我猜你的问题是由于从本地存储中存储和获取值。 负责文本外观的代码

$(document).ready(function(){
    document.getElementById("text1").value = localStorage.getItem("item1");
});

你为什么不清除本地存储?

$(window).unload(function() {
    window.localStorage.removeItem(item1);
});

代码未经测试,但这应该给你一个想法......

暂无
暂无

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

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