【问题标题】:How to retain textbox value after click submit button单击提交按钮后如何保留文本框值
【发布时间】:2016-04-18 09:19:48
【问题描述】:

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

<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> 

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

谢谢。

【问题讨论】:

  • 为什么不能从localStorage中删除value?
  • @Kalpesh Rajai,我怎么删除...??
  • localStorage.clear();@Rehan
  • 您将表单提交到另一个页面或同一页面?
  • 如果您将表单发布到同一页面,则无法实现您想要的。因为当您发布页面时,它会将数据发布到服务器并重新加载页面,并且在重新加载/刷新页面时以相同的方式重新加载......它的工作原理相同。

标签: javascript html


【解决方案1】:
        $(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>

【讨论】:

  • 我如何使用隐藏输入..??
  • 文本框在使用输入 type="hidden" 后被隐藏,还有其他建议吗.. ?
  • 试试上面的代码。这将在提交时保存,但如果您刷新它不会。此代码不使用隐藏字段。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    为什么不清除本地存储?

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 2021-01-13
      相关资源
      最近更新 更多