javascript - 错误未捕获类型错误 : Cannot read property 'checked' of null

标签 javascript jquery html css angular

当我点击复选框时,我需要显示一张图片。现在我收到一个错误:

Uncaught TypeError: Cannot read property 'checked' of null
at (index):185
at dispatch (VM576 jquery.min.js:3)
at i (VM576 jquery.min.js:3)

你能告诉我如何解决吗?我在下面提供我的代码。

http://jsfiddle.net/xda0kL6w/1/

if (document.getElementById('checkIDGrid').checked) {
  alert("I am checked for the grid");

  var img = $('<img />', {
    id: 'Myid',
    src: 'https://thumbs.dreamstime.com/z/pixel-perfect-web-development-flat-icons-set-website-programming-process-webpage-coding-user-interface-creating-45297890.jpg',
    alt: 'MyAlt'
  });
  img.appendTo($('<tr>'));

} else {
  alert("I am not checked for the grid");
}

最佳答案

像这样:

const option = document.getElementById('checkIDGrid');
if ( option && option.checked) {
    ...
}

关于javascript - 错误未捕获类型错误 : Cannot read property 'checked' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49655243/

相关文章:

javascript - 如何将删除/清除字段图标添加到输入字段

javascript - Jquery 移动幻灯片

php - php 中的 number_format() 与 jQuery 中的 toFixed() 进行验证

javascript - jQuery 删除中继器中的输入值

javascript - 单击时的 jQuery 导航 slider

html - Web 开发人员还在使用框架集吗?

javascript - 如何在div内圆 Angular 图像

javascript - 一致的设备ID

javascript - 在单击图像上打开模态(使用单个模态的多个图像)

javascript - 如何在 html/javascript 中创建 "copy to clipboard"按钮