我收到错误:未捕获类型错误:document.querySelector(..)在以下代码中为null


document.querySelector('button[type="submit"]').addEventListener('click', () => {
const inputTags = Array.from(document.querySelectorAll('input'));
const textArea = document.querySelector('textarea');
inputTags.splice(inputTags.length, 0, textArea);

let output = "";
inputTags.forEach((input) => {
if(input.type == "checkbox" && input.checked == true ) {
output += input.nextElementSibling.textContent; 
output += input.checked; 
output += "n"; 
} else if(input.type == "radio" && input.checked == true) {
if(input.nextElementSibling.textContent == "I have money and can pay for this") {
output += input.nextElementSibling.textContent.toLowerCase();
output += "n"; 
}
} else {
output += input.nextElementSibling.textContent; 
output += input.value;
}
})
alert(output);
})

这将返回null

document.querySelector('textarea');

如果你的html正文中没有<textarea />

<textarea id="example" name="exampleTextArea" rows="4" cols="50">Sample Text</textarea>

最新更新