我当前遇到的问题是无法将输入框设置为在刷新后保持选中状态。我已将值存储在本地存储中,即使该值存储在本地存储中;此复选框未保持选中状态。请帮助我解决此问题!谢谢
下面是HTML代码:
<div style="color: #005e95">
<input type="checkbox" id="checkboxFullDownload" data-dojo-attach-event="onClick: _test" style="cursor: pointer;" >
Full Download
</div>
下面是JS代码:
_test: function(){
let checkBox = document.getElementById("checkboxFullDownload")
if(localStorage.getItem(`${xmlTitle} ID:${this.item._id}`) === "AllowFullDownload"){
checkBox.setAttribute("checked",true)
console.log("set to true")
}
if(checkBox.checked){
console.log("Checked")
localStorage.setItem(`${xmlTitle} ID:${this.item._id}`,"AllowFullDownload")
checkBox.checked = true;
}
if(!checkBox.checked){
console.log("Unchecked")
localStorage.removeItem(`${xmlTitle} ID:${this.item._id}`)
checkBox.setAttribute("checked",false)
}
}
2条答案
按热度按时间uoifb46i1#
如果您想选中pagload上的复选框,那么
localStorage.getItem()
必须超出在某些事件上执行的任何函数的范围。zlwx9yxi2#
将
checkBox.setAttribute("checked",true)
替换为checkBox.checked = true