关于localStorage的使用,记住checkBox选中状态,刷新页面清空。
$(document).ready(function() {
$(function () {
//取得本地存储的被选中checkbox的个数,循环将checkbox选中
var length=localStorage.getItem('length');
for(var i=0;i<=length;i++){
var a=localStorage.getItem(i);
$("input:checkbox[value="+a+"]").attr("checked","checked");
}
//清空,避免刷新后checkbox还是选中状态
localStorage.clear();
});
});
function clickButton(){
var checked=$('input[name="statusList"]:checked');
checked.each(function (i) {
localStorage.setItem(i,$(this).val());
localStorage.setItem('length',i);
});
}
<label id="projectStatus"> 状态:</label>
<input type = "checkbox" id="statusList[0]" name="statusList" value="1" ><label for="statusList[0]">未启动 </label>
<input type = "checkbox" id="statusList[1]" name="statusList" value="2" ><label for="statusList[1]">进行中 </label>
<input type = "checkbox" id="statusList[2]" name="statusList" value="3" ><label for="statusList[2]">已结项 </label>
<input type="submit" id="btnSubmit"class="btn btn-primary" value="查 询" onclick = "clickButton()"/>