我有一个下拉菜单有几个选项。在页面刷新时,我需要再次选择之前选择的选项。
这是我的javascript。
按下按钮会在页面加载时启动 saveQ1(id) 函数,它会启动 loadQ1() 函数
//Save answer 1 to LocalStorage
function saveQ1(id) {
var answer = [document.getElementById("answer1").selectedIndex].value;
localStorage.setItem('savequestion4656290', answer);
}
function loadQ1() {
//if something was already saved....
if (localStorage.getItem('savequestion4656290')) {
var answer = localStorage.getItem('savequestion4656290');
} else {
var answer = ' ';
}
//Put Answer 1 back in to box
//I NEED SOMETHING HERE WHICH CHOOSES THE CORRECT OPTION (as remembered by local storage)
}
最后部分应该做什么才能使先前保存的选择显示。我什至不确定它是否正确保存。谢谢