javascript - 未捕获的类型错误:无法在 addEventListener 处读取 null 的属性“addEventListener”
问题描述
我在这里遗漏了一些非常明显的东西,这是我第一次尝试 Javascript。我正在尝试单击单选按钮来填写一些表格,并且代码都非常简单......
Firefox 说 streetInput 为空。当我单击单选按钮时会发生这种情况。Streetinput 是要填写的表单的 ID。
textContent 应该使用给定的文本填写表单,但事实并非如此。没有一个。
Chrome 说 script.js:37 Uncaught TypeError: Cannot read property 'addEventListener' of null
var homeOption = document.querySelector(".homeoption");
var workOption = document.querySelector(".workoption");
var streetInput = document.querySelector(".streetinput");
var cityInput = document.querySelector(".cityinput");
var stateInput = document.querySelector(".stateinput");
var zipInput = document.querySelector(".zipinput");
function HomeFillout() {
streetInput.textContent = "1 Main St.";
cityInput.textContent = "Sicilia";
stateInput.textContent = "ME";
zipInput.textContent = "03900";
if (form.checkValidity() === true) {
submitButton.className = "submitbutton show";
}
}
function WorkFillout() {
streetInput.textContent = "15 Columbine Ln.";
cityInput.textContent = "Crab City";
stateInput.textContent = "ME";
zipInput.textContent = "04993";
if (form.checkValidity() === true) {
submitButton.className = "submitbutton show";
}
}
homeOption.addEventListener("click", HomeFillout, false);
workOption.addEventListener("click", WorkFillout, false);
解决方案
推荐阅读
- django - Django - 如何在同一页面中使多个对象动态化
- javascript - 是否可以在节点 js 中操作 DOM?
- r - data.table 中用于转换日期的 mutate_at (dplyr) 替代方案
- python - 安排一个 python 脚本每小时运行一次
- excel - Msgbox 只显示一次
- python-3.x - 装饰器意外更改 Python 中的构造函数行为
- r - 网页抓取:在 R 中的 for 循环中组合表格
- c# - 什么 IEnumerable
> 是什么意思? - apache-camel - 如何在 Camel JMS Route 中设置事务
- mysql - 如何使用 Django、Django Rest Framework 和 mysql 创建参加活动的人员列表