Javascript 未捕获的类型错误:无法将属性“已检查”设置为 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31634528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Cannot set property 'checked' of null
提问by Peter
I am using a Javascript at javascript Hide/show div on checkbox: checked/unchecked.
我在javascript Hide/show div on checkbox: checked/unchecked 上使用 Javascript 。
var elem = document.getElementById('powermail_fieldwrap_331'),
checkBox = document.getElementById('powermail_field_doruovaciaadresa2_1');
checkBox.checked = false;
checkBox.onchange = function doruc() {
elem.style.display = this.checked ? 'block' : 'none';
};
checkBox.onchange();
I changed "checkBox.checked = false;" to "checkBox.checked = true;" It works anyway but produces an error: Uncaught TypeError: Cannot set property 'checked' of null
我改变了“checkBox.checked = false;” 到“checkBox.checked = true;” 无论如何它都可以工作,但会产生错误:未捕获的类型错误: 无法设置 null 的属性“已检查”
In this case, how I fix this error?
在这种情况下,我如何解决此错误?
Thanks.
谢谢。
回答by Sebastian Nette
The fiddle works fine. The only issue can be that the script is executed before the checkbox was part of the DOM.
小提琴工作正常。唯一的问题可能是脚本在复选框成为 DOM 的一部分之前执行。
Either wrap your code within an onload
event.
要么将您的代码包装在一个onload
事件中。
window.onload = function() {
var elem = document.getElementById('powermail_fieldwrap_331'),
checkBox = document.getElementById('powermail_field_doruovaciaadresa2_1');
checkBox.checked = false;
checkBox.onchange = function doruc() {
elem.style.display = this.checked ? 'block' : 'none';
};
checkBox.onchange();
};
Or make sure that the script tag is written after the checkbox is part of your DOM:
或者确保在复选框是 DOM 的一部分之后编写脚本标记:
<label>
<input type="checkbox" id="powermail_field_doruovaciaadresa2_1" checked="checked" />
Show/hide
</label>
<div id="powermail_fieldwrap_331">Lorem ipsum</div>
<script>
var elem = document.getElementById('powermail_fieldwrap_331'),
checkBox = document.getElementById('powermail_field_doruovaciaadresa2_1');
checkBox.checked = true;
checkBox.onchange = function() {
elem.style.display = this.checked ? 'block' : 'none';
};
checkBox.onchange();
</script>
回答by Barr J
Your code executes, before the document is fully ready, which is causing you to receive the ID as null, since it is not rendered yet.
您的代码在文档完全准备好之前执行,这导致您收到的 ID 为 null,因为它尚未呈现。
It is a common problem, this is why in jquery you always use $(document).ready();
这是一个常见的问题,这就是为什么在 jquery 中你总是使用 $(document).ready();
To make sure, that the document is fully loaded first and no elements will be executed before the document is fully loaded, preventing errors like this.
为了确保首先完全加载文档,并且在文档完全加载之前不会执行任何元素,从而防止出现此类错误。
There is a very helpful post regarding this in java script:
在java脚本中有一个非常有用的帖子:
回答by thaimechatronics
You need to change all of tag without checked by
您需要在不检查的情况下更改所有标签
回答by rinku Choudhary
Your document.getElementById("checkboxid") contains null value. For that store it in a variable and then you can check like
您的 document.getElementById("checkboxid") 包含空值。为此,将其存储在一个变量中,然后您可以检查
var checkbox = document.getElementById("chkteam");
if (checkbox == true) {
// your condition
}
Then It will Work.
然后它会工作。