我有几个选项下拉。在页面刷新时,我需要先前选择再次选择的选项。
这是我的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)
}
最后部分应该放置什么才能显示以前保存的选项。 我甚至不确定它的保存是否正常。感谢
答案 0 :(得分:1)
如果我正确阅读此内容,您需要添加一行
var answer1 = document.getElementByID("answer1");
answer1.selectedIndex = answer