Retaining the textbox values even after refresh

前端 未结 3 849
一向
一向 2021-01-26 10:25

I want to Retain the textbox values even after Refresh of page rather it is successfully submitted.

Using cookies or using php sessions

Here is the code Below :

相关标签:
3条回答
  • 2021-01-26 11:00
        <script>
          $("#target").change(function(){
    
     if($(this).is(":checked")) {
                if (localStorage['first-name']) {
        $("input[name='first-name']").val(localStorage['first-name']);
        }
        if (localStorage['last-name']) {
        $("input[name='last-name']").val(localStorage['last-name']);
        }
        if (localStorage['your-email']) {
        $("input[name='your-email']").val(localStorage['your-email']);
        }
        }
        });
        $(".wpcf7-text").on("change",function () {
        //store vlues in session
        localStorage.setItem($(this).attr("name") , $(this).val());
        });
        </script>
    
    0 讨论(0)
  • 2021-01-26 11:01

    Oh yea!! You can use localStorage or sessionStorage of HTML5.

    sessionStorage will keep it until a session is alive but localStorage will keep it until you delete it manually.

    See below for ways you can store it in either of them:

    To Store:

    localStorage.setItem('somekey','textboxValue') 
    //or 
    sessionStorage.setItem('somekey','textboxvalue')
    

    To Retrieve

    localStorage.getItem('somekey')
    //or
    sessionStorage.getItem('somekey');
    
    0 讨论(0)
  • 2021-01-26 11:16

    You could solve this using jQuery and local storage.

    This has been answered here: Saving form data to local storage and show it on refresh

    It basically runs a load/save command when the page changes.

    0 讨论(0)
提交回复
热议问题