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

How to retain textbox value after click submit button

本文关键字:保留 文本 按钮 提交 何保留 单击      更新时间:2023-09-26

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

<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);
});

代码没有经过测试,但这应该会给你一个想法。。。