dojo 刷新并存储到本地存储后,无法将复选框设置为“选中

b5lpy0ml  于 2022-12-16  发布在  Dojo
关注(0)|答案(2)|浏览(147)

我当前遇到的问题是无法将输入框设置为在刷新后保持选中状态。我已将值存储在本地存储中,即使该值存储在本地存储中;此复选框未保持选中状态。请帮助我解决此问题!谢谢
下面是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)
          }
}
uoifb46i

uoifb46i1#

如果您想选中pagload上的复选框,那么localStorage.getItem()必须超出在某些事件上执行的任何函数的范围。

let checkBox = document.getElementById("checkboxFullDownload");
_test: function() {
  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.checked = false;
  }
}
if (localStorage.getItem(`${xmlTitle} ID:${_test.item._id}`) === "AllowFullDownload") {
    checkBox.checked = true;
    console.log("set to true");
}
zlwx9yxi

zlwx9yxi2#

checkBox.setAttribute("checked",true)替换为checkBox.checked = true

相关问题