restore dropdown selection after refresh local storage

前端 未结 3 1569
眼角桃花
眼角桃花 2021-01-03 12:42

I am trying to save an options tag value to local storage. After saving its value to local storage I would like to be able to set the options tag to the option the user sele

3条回答
  •  有刺的猬
    2021-01-03 13:23

    Its because your values and text are different for your select options. Try:

     
    
        document.getElementById("fruit").onchange = function() {
         localStorage['fruit'] = document.getElementById("fruit").value;
        }
        window.onload= function(){
            if(localStorage['fruit'])
                document.getElementById("fruit").value = localStorage['fruit'];
        }
    

提交回复
热议问题