在使用本地存储进行刷新后,我正在尝试将所选选项保留在HTML中。我按照提到的例子here 以下是我的代码。
document.getElementById("interface-output").onchange = function() {
localStorage.setItem('selectedtem', document.getElementById("interface-output").value);
};
if (localStorage.getItem('item')) {
document.getElementById("selectedtem").options[localStorage.getItem('selectedtem')].selected = true;
}
<div class="row" id="ott-redirect-interface-selector">
<label>Output Interface</label><br>
<select id="interface-output" class="browser-default">
<option value="select">Select an interface</option>
<option value="eth0">eth0</option>
<option value="eth1">eth1</option>
</select>
</div>
不幸的是,此设置在刷新后不会保留所选选项。有什么建议吗?
答案 0 :(得分:3)
<html>
<body>
<div class="row" id="ott-redirect-interface-selector">
<label>Output Interface</label><br>
<select id="interface-output" class="browser-default">
<option id="select" value="select">Select an interface</option>
<option id="eth0" value="eth0">eth0</option>
<option id="eth1" value="eth1">eth1</option>
</select>
</div>
<script>
document.getElementById('interface-output').onchange = function() {
localStorage.setItem('selectedtem', document.getElementById('interface-output').value);
};
if (localStorage.getItem('selectedtem')) {
document.getElementById('interface-output').options[localStorage.getItem('selectedtem')].selected = true;
}
</script>
</body>
</html>