我是HTML和Javascript的新手。我正在创建一个基本的登录屏幕。
当我试图在控制台打印电子邮件的值和密码时,我什么也没得到。有人能帮我吗?
document.querySelector('.form').addEventListener('submit', (e) => {
e.preventDefault();
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
console.log(email, password);
});
<div class="login-form">
<h2 class="heading-secondary ma-bt-lg">Log into your account</h2>
<form class="form"></form>
<div class="form__group">
<label class="form__label" for="email">Email address</label
><input
class="form__input"
id="email"
type="email"
placeholder="you@example.com"
required=""
/>
</div>
<div class="form__group ma-bt-md">
<label class="form__label" for="password">Password</label
><input
class="form__input"
id="password"
type="password"
placeholder="••••••••"
required=""
minlength="8"
/>
</div>
<div class="form__group">
<button class="btn btn--green">Login</button>
</div>
</div>
<!-- <script src="/js/login.js"></script> -->
3条答案
按热度按时间6ss1mwsb1#
这是因为
<form class="form"></form>
是空的,你应该把输入放在里面。bn31dyow2#
fhg3lkii3#
处理HTML表单的其他方法: