无法从HTML表单中读取事件内容

huangapple go评论71阅读模式
英文:

Unable to read event Content from a form in HTML

问题

我是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>

希望对你有所帮助。

英文:

I am new to HTML and Javascript. I was working on creating a basic login screen.

无法从HTML表单中读取事件内容

As I am trying to print the value of the email and the password in the console, I get nothing. Can someone help me out?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.querySelector(&#39;.form&#39;).addEventListener(&#39;submit&#39;, (e) =&gt; {
  e.preventDefault();
  const email = document.getElementById(&#39;email&#39;).value;
  const password = document.getElementById(&#39;password&#39;).value;
  console.log(email, password);
});

<!-- language: lang-html -->

&lt;div class=&quot;login-form&quot;&gt;
  &lt;h2 class=&quot;heading-secondary ma-bt-lg&quot;&gt;Log into your account&lt;/h2&gt;
  &lt;form class=&quot;form&quot;&gt;&lt;/form&gt;
  &lt;div class=&quot;form__group&quot;&gt;
    &lt;label class=&quot;form__label&quot; for=&quot;email&quot;&gt;Email address&lt;/label
        &gt;&lt;input
          class=&quot;form__input&quot;
          id=&quot;email&quot;
          type=&quot;email&quot;
          placeholder=&quot;you@example.com&quot;
          required=&quot;&quot;
        /&gt;
      &lt;/div&gt;
      &lt;div class=&quot;form__group ma-bt-md&quot;&gt;
        &lt;label class=&quot;form__label&quot; for=&quot;password&quot;&gt;Password&lt;/label
        &gt;&lt;input
          class=&quot;form__input&quot;
          id=&quot;password&quot;
          type=&quot;password&quot;
          placeholder=&quot;••••••••&quot;
          required=&quot;&quot;
          minlength=&quot;8&quot;
        /&gt;
      &lt;/div&gt;
      &lt;div class=&quot;form__group&quot;&gt;
        &lt;button class=&quot;btn btn--green&quot;&gt;Login&lt;/button&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    
    &lt;!-- &lt;script src=&quot;/js/login.js&quot;&gt;&lt;/script&gt; --&gt;

<!-- end snippet -->

答案1

得分: 2

这是因为&lt;form class=&quot;form&quot;&gt;&lt;/form&gt;为空,你应该将输入放在其中。

&lt;div class=&quot;login-form&quot;&gt;
    &lt;h2 class=&quot;heading-secondary ma-bt-lg&quot;&gt;登录到您的帐户&lt;/h2&gt;
    &lt;form class=&quot;form&quot;&gt;
        &lt;div class=&quot;form__group&quot;&gt;&lt;label class=&quot;form__label&quot; for=&quot;email&quot;&gt;电子邮件地址&lt;/label&gt;&lt;input class=&quot;form__input&quot; id=&quot;email&quot; type=&quot;email&quot; placeholder=&quot;you@example.com&quot; required=&quot;&quot; /&gt;&lt;/div&gt;
        &lt;div class=&quot;form__group ma-bt-md&quot;&gt;&lt;label class=&quot;form__label&quot; for=&quot;password&quot;&gt;密码&lt;/label&gt;&lt;input class=&quot;form__input&quot; id=&quot;password&quot; type=&quot;password&quot; placeholder=&quot;••••••••&quot; required=&quot;&quot; minlength=&quot;8&quot; /&gt;&lt;/div&gt;
        &lt;div class=&quot;form__group&quot;&gt;
            &lt;button class=&quot;btn btn--green&quot;&gt;登录&lt;/button&gt;
        &lt;/div&gt;
    &lt;/form&gt;
&lt;/div&gt;
英文:

That's because &lt;form class=&quot;form&quot;&gt;&lt;/form&gt; is empty, you should put the inputs inside it.

&lt;div class=&quot;login-form&quot;&gt;
    &lt;h2 class=&quot;heading-secondary ma-bt-lg&quot;&gt;Log into your account&lt;/h2&gt;
    &lt;form class=&quot;form&quot;&gt;
        &lt;div class=&quot;form__group&quot;&gt;&lt;label class=&quot;form__label&quot; for=&quot;email&quot;&gt;Email address&lt;/label&gt;&lt;input class=&quot;form__input&quot; id=&quot;email&quot; type=&quot;email&quot; placeholder=&quot;you@example.com&quot; required=&quot;&quot; /&gt;&lt;/div&gt;
        &lt;div class=&quot;form__group ma-bt-md&quot;&gt;&lt;label class=&quot;form__label&quot; for=&quot;password&quot;&gt;Password&lt;/label&gt;&lt;input class=&quot;form__input&quot; id=&quot;password&quot; type=&quot;password&quot; placeholder=&quot;••••••••&quot; required=&quot;&quot; minlength=&quot;8&quot; /&gt;&lt;/div&gt;
        &lt;div class=&quot;form__group&quot;&gt;
            &lt;button class=&quot;btn btn--green&quot;&gt;Login&lt;/button&gt;
        &lt;/div&gt;
    &lt;/form&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年2月14日 05:15:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定