简体   繁体   English

复选框问题 - 无法设置 null 属性(设置“已选中”)

[英]Checkbox Issue- Cannot set properties of null (setting 'checked')

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).基本上我想通过使用我存储在 localStorage 中的键值对值来检查我的复选框,所以在重新加载时应该检查复选框(那些具有键值对的)。 However, I kept getting this error.但是,我一直收到此错误。 I have tried using window.onLoad but it did not work;我曾尝试使用 window.onLoad 但它不起作用; 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: html代码:

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

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

let keyword is block scope once declared in scope cannot be accessed outside of that scope. let关键字是块作用域,一旦在作用域中声明,就不能在该作用域之外访问。

In your case let keyword is in the scope of _test: function(){} .在您的情况下, let关键字在_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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法为CheckBox设置null的&#39;checked&#39;属性 - TypeError: Cannot set property 'checked' of null for CheckBox 无法设置 null 的属性(设置“禁用”) - Cannot set properties of null (setting 'disabled') 无法设置 null 的属性(设置“图像”) - Cannot set properties of null (setting 'image') 无法设置 null 的属性(设置“innerHTML”) - Cannot set properties of null (setting 'innerHTML') 类型错误:无法在 javascript 中设置未定义的属性(设置“选中”) - TypeError: Cannot set properties of undefined (setting 'checked') in javascript For循环问题? Uncaught (in promise) TypeError: cannot set properties of null (setting 'inner text) - For-loop issue? Uncaught (in promise) TypeError: cannot set properties of null (setting 'inner text) 如何在反应中解决无法设置 null 的属性(设置“内部 HTML”)? - how to solve in react Cannot set properties of null (setting 'inner HTML')? 未捕获的类型错误:无法设置 null 的属性(设置“onclick”)错误 - Uncaught TypeError: Cannot set properties of null (setting 'onclick') error 无法设置预加载脚本中使用的 ID 的 null 的属性(设置“隐藏”) - Cannot set properties of null (setting 'hidden') of an ids used in preload script 显示无法设置 Null 设置的属性 (innerHtml)。 在替换文本 - Showing Cannot Set the Properties Of Null setting (innerHtml). at replaceText
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM