获取“未接收参考:未定义形式”。错误

发布于 2025-01-20 17:26:50 字数 755 浏览 0 评论 0原文

我得到的是“未定义的参考文献:形式未定义”。

However, the "form is defined in an object. I am not going to post the entire JS code just where the form is declared.

line 6- const elements = 
line 7- {
line 8-     form: document.querySelector("#new-task-form"),
line 9-     input: document.querySelector("#new-task-input"),
line 10-    list: document.querySelector("#tasks"),
line 11-    cal:document.querySelector("#calendar")
line 12- }

line 91- const submitHandler = (event) =>{
line 92-    event.preventDefault();
line 93-    createTask();
line 94- }

line 96 - form.addEventListener("submit", submitHandler);

The form.addEventListener("submit", submitHandler); is calling back subsithandler的功能。

I am getting the "Uncaught ReferenceError: form is not defined".

However, the "form is defined in an object. I am not going to post the entire JS code just where the form is declared.

line 6- const elements = 
line 7- {
line 8-     form: document.querySelector("#new-task-form"),
line 9-     input: document.querySelector("#new-task-input"),
line 10-    list: document.querySelector("#tasks"),
line 11-    cal:document.querySelector("#calendar")
line 12- }

line 91- const submitHandler = (event) =>{
line 92-    event.preventDefault();
line 93-    createTask();
line 94- }

line 96 - form.addEventListener("submit", submitHandler);

The form.addEventListener("submit", submitHandler); is calling back the function of submitHandler. So not sure why it is giving me the error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

垂暮老矣 2025-01-27 17:26:50

我想通了这个问题。

const createTask = () => {
    const id = createId()
    const task = elements.input.value;
    const date = elements.cal.value;

    if(!task && !date) return alert("Please fill in task and select date");
    if(!task) return alert("Please fill in task");
    if(!date) return alert("Please select date");

    const tasks = document.createElement("div");

    tasks.innerHTML = `
    <div class="task" date-id = "${id}">
        <div class="content">
            <input type ="checkbox" class="tick">
            <input class = text id = "text" readonly>
            <label class = "due-date" for ="text">${date}</label>
        </div>

        <div class = "action">
            <button class="edit" data-id="${id}>Edit</button>
            <button class="delete" data-id="${id}>Delet</button>
        </div>
    </div>
    `
    elements.list.appendChild(tasks)
    return task
}

问题是 const task = elements.input.value 定义不正确,我将其定义为 const task = input.value

I figured out the issue.

const createTask = () => {
    const id = createId()
    const task = elements.input.value;
    const date = elements.cal.value;

    if(!task && !date) return alert("Please fill in task and select date");
    if(!task) return alert("Please fill in task");
    if(!date) return alert("Please select date");

    const tasks = document.createElement("div");

    tasks.innerHTML = `
    <div class="task" date-id = "${id}">
        <div class="content">
            <input type ="checkbox" class="tick">
            <input class = text id = "text" readonly>
            <label class = "due-date" for ="text">${date}</label>
        </div>

        <div class = "action">
            <button class="edit" data-id="${id}>Edit</button>
            <button class="delete" data-id="${id}>Delet</button>
        </div>
    </div>
    `
    elements.list.appendChild(tasks)
    return task
}

The issue was that const task = elements.input.value, was defined incorrectly, i had it as const task = input.value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文