[英]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.