将复选框设置为Chrome中的localStorage值

时间:2014-11-12 23:11:29

标签: javascript google-chrome checkbox local-storage

我似乎无法在Chrome中使用localStorage设置复选框的选中状态。

她是我的测试代码。

<script>
function setStorage() {
localStorage.check = document.getElementById("bike").checked;
alert(localStorage.check);
}

</script>

<script>
function onLoaded() {
document.getElementById("bike").checked = localStorage.check
alert(localStorage.check)
}
</script>

<body onload="onLoaded()">
<form action="">
<input type="checkbox" id="bike" name="vehicle" value="Bike">I have a bike 
</form>
<button onclick="setStorage()">setStorage</button>
</body>

2 个答案:

答案 0 :(得分:0)

试试这个,

localStorage.setItem("bike", document.getElementById("bike").checked)

您可以通过

检索值
localStorage.getItem("bike")

答案 1 :(得分:0)

想出来。这是代码。

<script>
function setStorage() {


localStorage.check = document.getElementById("bike").checked;
alert(localStorage.check);


}

</script>
<script>
function onLoaded() {
if (localStorage.check == 'true') {
document.getElementById("bike").checked = true;
alert("so far so good");
}
}


</script>


<body onload="onLoaded()">
<form action="">
<input type="checkbox" id="bike" name="vehicle" value="Bike">I have a bike 
</form>
<button onclick="setStorage()">setStorage</button>