我想提交HTML表单并根据输入和文本区域中的值读取消息.然而,我收到这个TypHelp告诉我事件监听器为空,尽管它不是.有人能看出我哪里做错了吗?
let recommendationForm = document.getElementById("recommendationForm");
recommendationForm.addEventListener("submit", (e) => {
e.preventDefault();
let name = document.getElementById("name");
let recommendation = document.getElementById("recommendation");
if (name.value == "" || recommendation.value == "") {
alert("Input a value in both fields!");
} else {
alert("Recommendation submitted successfully!");
console.log(`${name.value} submitted their recommendation`);
};
name.value = "";
recommendation.value = "";
});
<form action="" id="recommendationForm" class="recommendationSubmission">
<input type="text" id="name" name="name" placeholder="Name" autocapitalize="on"/>
<textarea type="text" id="recommendation" placeholder="Recommendation" rows="5" cols="45" autocapitalize="on" wrap="hard"></textarea>
<button type="submit" id="button">Submit</button>
</form>