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