复选框问题 - 无法设置null的属性(设置' checked')

发布于 2025-02-12 03:07:49 字数 1100 浏览 2 评论 0原文

大家好的一天,我目前在以下代码中面临一些问题。基本上,我想通过使用我存储在LocalStorage的键值对值来检查复选框,因此在重新加载时应检查复选框(具有键值对的复选框)。但是,我一直遇到这个错误。 我尝试使用window.onload,但它不起作用。还有其他解决方案。请让我知道我的代码是否有任何问题。谢谢您的

错误

Cannot set properties of null (setting 'checked')

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 (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");
}

Good day everyone, I am currently facing some issues with my following code. Basically I want to keep my checkbox checked by using the key value pair value I stored in localStorage, so upon reload the checkbox should be checked (those with the key value pair). However, I kept getting this error.
I have tried using window.onLoad but it did not work; any other solutions for this..? Please do let me know if there is anything wrong with my code too. Thank you

Error

Cannot set properties of null (setting 'checked')

Html Code:

<div style="color: #005e95">
        <input type="checkbox" id="checkboxFullDownload" data-dojo-attach-event="onClick: _test" style="cursor: pointer;"  >
        Full Download
      </div>

JS Code:

_test: function() {
  let checkBox = document.getElementById("checkboxFullDownload");

  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");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅忆 2025-02-19 03:07:49

关键字是块范围,一旦在该范围之外无法访问范围内声明。

在您的情况下,关键字位于_test的范围:function(){}。通过将其移到外面将完成工作。

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");
}

let keyword is block scope once declared in scope cannot be accessed outside of that scope.

In your case let keyword is in the scope of _test: function(){}. By moving it outside will do the work.

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文