我使用本地存储来存储员工的活动日志。他们从下拉框中选择他们的活动,他们选择的活动在Firefox中刷新后保留在下拉框中,但在Google Chrome中恢复为默认值。 Chrome中是否有我需要更改的设置才能保留所选的活动?这是我的一名员工的代码。
<script>
function foo1(a) {
//alert(a.value);
var d = new Date();
var c = document.getElementById("time1").innerHTML;
c = c + a.value + " was selected at: " + d.toString();
document.getElementById("time1").innerHTML = c + '<br/>';
localStorage.setItem(a.value, d.toString());
}
function load1() {
var c = "Wholesale Pictures.: " + localStorage.getItem("Wholesale Pictures.") +
'<br/>';
c += "NS Pictures.:" + localStorage.getItem("NS Pictures.") + '<br/>';
c += "NS ERO's.:" + localStorage.getItem("NS ERO's.") + '<br/>';
c += "BL ERO's.:" + localStorage.getItem("BL ERO's.") + '<br/>';
c += "Wholesale Staging.:" + localStorage.getItem("Wholesale Staging.") + '<br/>';
c += "Wholesale Scan.:" + localStorage.getItem("Wholesale Scan.") + '<br/>';
c += "Sirius Tags.:" + localStorage.getItem("Sirius Tags.") + '<br/>';
c += "Lunch Break.:" + localStorage.getItem("Lunch Break.") + '<br/>';
document.getElementById("log1").innerHTML = c;
}
</script>
<div class="david">
<h2><img src="Images/david.jpg" class="img-circle">  David</h2>
<p id="log1"></p>
<select id="options" class="form-control" onchange="foo1(this)">
<option>Select Work Area</option>
<option>NS Pictures.</option>
<option>NS ERO's.</option>
<option>BL ERO's.</option>
<option>Wholesale Staging.</option>
<option>Wholesale Scan.</option>
<option>Wholesale Pictures.</option>
<option>Sirius Tags.</option>
<option>Lunch Break.</option>
</select>
<div class="buttons">
<button id="log1" onclick="load1()">Activity Log</button>
</div>
<p id="time1"><br/></p>
</div>
答案 0 :(得分:0)
该问题与本地存储无关,而是与软刷新页面时chrome和firefox处理表单值的方式不同。 Firefox非常好,可以记住表单的最后一个值,并且chrome不是。您的代码中没有任何内容试图在页面加载时将下拉列表设置为上次选择的值。您必须在代码中添加一些内容才能执行此操作。您可以将最后选择的值存储到本地存储,然后在页面加载时检索它,如下所示:
foo1(a) {
//your code...
localStorage.setItem('lastViewed',a.value);
}
然后在页面底部
<script>
var value=localStorage.getItem('lastViewed');
if (typeof value === "string")
{
document.getElementById('options').value=value;
}
</script>