提交按钮不起作用!单击它甚至没有console.log

问题描述

我正在使用顺风CSS制作登录页面,但提交按钮不起作用。我试图检查console.log甚至不起作用!

这是HTML部分

<section id="Contacts" class="text-shark-100 bg-shark-500 body-font relative">
        <div class="container px-5 pt-24 pb-15 mx-auto">
          <div class="flex flex-col text-center w-full mb-12">
            <h1 class="sm:text-3xl text-2xl font-medium title-font mb-4 text-white">Contact Us</h1>
            <p class="lg:w-2/3 mx-auto leading-relaxed text-base">Have any Feedbacks & Questions,Just Go Ahead!</p>
          </div>
          <div class="lg:w-1/2 md:w-2/3 mx-auto">
            <div class="flex flex-wrap -m-2" id="Contact">
              <div class="p-2 w-1/2">
                <input class="w-full bg-shark-400 rounded border border-shark-300 text-white focus:outline-none focus:border-red-500 text-base px-4 py-2" placeholder="First Name" id="FirstName" type="text">
              </div>
              <div class="p-2 w-1/2">
                <input class="w-full bg-shark-400 rounded border border-shark-300 text-white focus:outline-none focus:border-red-500 text-base px-4 py-2" placeholder="Last Name" id="LastName" type="text">
              </div>
              <div class="p-2 w-full">
                <input class="w-full bg-shark-400 rounded border border-shark-300 text-white focus:outline-none focus:border-red-500 text-base px-4 py-2" placeholder="Company" id="Company" type="Text">
              </div>
              <div class="p-2 w-full">
                <input class="w-full bg-shark-400 rounded border border-shark-300 text-white focus:outline-none focus:border-red-500 text-base px-4 py-2" placeholder="Email" id="Email" type="email">
              </div>
              <div class="p-2 w-full">
                <input class="w-full bg-shark-400 rounded border border-shark-300 text-white focus:outline-none focus:border-red-500 text-base px-4 py-2" placeholder="Phone Number" id="PhoneNumber" type="tel">
              </div>
              <div class="p-2 w-full">
                <textarea class="w-full bg-shark-400 rounded border border-shark-300 text-white focus:outline-none h-48 focus:border-red-500 text-base px-4 py-2 resize-none block" placeholder="Message" id="Message"></textarea>
              </div>
              <div class="p-2 w-full"><!-- Trouble of the button -->
                <button class="flex mx-auto text-white bg-red-500 border-0 py-2 px-8 focus:outline-none hover:bg-red-600 hover:shadow-2xl rounded text-lg" type="submit">Let's Talk</button>
              </div>
              <div class="p-2 w-full pt-8 mt-8 border-t border-shark-300">
              </div>
            </div>
          </div>
        </div>
      </section>

javascript

document.getElementById('Contact').addEventListener('submit',submitForm);
function submitForm(e){
  e.preventDefault();
  console.log(123)
}

没有console.log或没有错误

更新:谢谢!更改为

解决方法

您将必须使用表单标签代替部分。

我可以看到ID错误。

应该是通过html模板查看的联系人。

,

查看此代码段:

document.addEventListener('DOMContentLoaded',(event) => {
            document.getElementById('contact-form').addEventListener('submit',submitForm);
            function submitForm(e){
                e.preventDefault();
                console.log('123');
            }
        });
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
</head>
<body>
    <section id="Contacts" class="text-shark-100 bg-shark-500 body-font relative">
        <div class="container px-5 pt-24 pb-15 mx-auto">
            <div class="flex flex-col text-center w-full mb-12">
                <h1 class="sm:text-3xl text-2xl font-medium title-font mb-4 text-white">Contact Us</h1>
                <p class="lg:w-2/3 mx-auto leading-relaxed text-base">Have any Feedbacks & Questions,Just Go Ahead!</p>
            </div>
            <div class="lg:w-1/2 md:w-2/3 mx-auto">

                <div class="flex flex-wrap -m-2" id="Contact">
                    <form id="contact-form">
                        <div class="p-2 w-1/2">
                            <input class="w-full bg-shark-400 rounded border border-shark-300 text-white focus:outline-none focus:border-red-500 text-base px-4 py-2" placeholder="First Name" id="FirstName" type="text">
                        </div>
                        <div class="p-2 w-1/2">
                            <input class="w-full bg-shark-400 rounded border border-shark-300 text-white focus:outline-none focus:border-red-500 text-base px-4 py-2" placeholder="Last Name" id="LastName" type="text">
                        </div>
                        <div class="p-2 w-full">
                            <input class="w-full bg-shark-400 rounded border border-shark-300 text-white focus:outline-none focus:border-red-500 text-base px-4 py-2" placeholder="Company" id="Company" type="Text">
                        </div>
                        <div class="p-2 w-full">
                            <input class="w-full bg-shark-400 rounded border border-shark-300 text-white focus:outline-none focus:border-red-500 text-base px-4 py-2" placeholder="Email" id="Email" type="email">
                        </div>
                        <div class="p-2 w-full">
                            <input class="w-full bg-shark-400 rounded border border-shark-300 text-white focus:outline-none focus:border-red-500 text-base px-4 py-2" placeholder="Phone Number" id="PhoneNumber" type="tel">
                        </div>
                        <div class="p-2 w-full">
                            <textarea class="w-full bg-shark-400 rounded border border-shark-300 text-white focus:outline-none h-48 focus:border-red-500 text-base px-4 py-2 resize-none block" placeholder="Message" id="Message"></textarea>
                        </div>
                        <div class="p-2 w-full"><!-- Trouble of the button -->
                            <button class="flex mx-auto text-white bg-red-500 border-0 py-2 px-8 focus:outline-none hover:bg-red-600 hover:shadow-2xl rounded text-lg" type="submit">Let's Talk</button>
                        </div>
                        <div class="p-2 w-full pt-8 mt-8 border-t border-shark-300">
                        </div>
                    </form>
                </div>
            </div>
            
        </div>
    </section>

</body>
</html>

需要HTML <form>来提交/发送信息。在JavaScript部分,首先我们寻找要加载的DOM Content。完成后,我已将您的代码添加到刚刚创建的表单中。

此脚本有2个选项,或者像我在代码段中所做的那样,可以在<head>内添加脚本,也可以在<form>之后添加此脚本,在这种情况下,我们不需要检查DOM内容加载事件。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...