Keep input value after refresh page

后端 未结 2 592
谎友^
谎友^ 2020-11-29 07:50

I have a form with input field and this input contain a drop down menu read information from database. If the user enters value and when he arrives to the drop menu he doesn

相关标签:
2条回答
  • 2020-11-29 08:14

    EDIT: Keep value of more inputs

    HTML:

    <input type="text" id="txt_1" onkeyup='saveValue(this);'/> 
    <input type="text" id="txt_2" onkeyup='saveValue(this);'/> 
    

    Javascript:

    <script type="text/javascript">
            document.getElementById("txt_1").value = getSavedValue("txt_1");    // set the value to this input
            document.getElementById("txt_2").value = getSavedValue("txt_2");   // set the value to this input
            /* Here you can add more inputs to set value. if it's saved */
    
            //Save the value function - save it to localStorage as (ID, VALUE)
            function saveValue(e){
                var id = e.id;  // get the sender's id to save it . 
                var val = e.value; // get the value. 
                localStorage.setItem(id, val);// Every time user writing something, the localStorage's value will override . 
            }
    
            //get the saved value function - return the value of "v" from localStorage. 
            function getSavedValue  (v){
                if (!localStorage.getItem(v)) {
                    return "";// You can change this to your defualt value. 
                }
                return localStorage.getItem(v);
            }
    </script>
    
    0 讨论(0)
  • 2020-11-29 08:23

    if the above code did not work try this:

    <input type="text" id="txt_1" onchange='saveValue(this);'/> 
    <input type="text" id="txt_2" onchange='saveValue(this);'/>
    
    0 讨论(0)
提交回复
热议问题