首页 > 解决方案 > 未捕获的类型错误:无法在 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);

标签: javascript

解决方案


推荐阅读