Attempting to .push() these errror messages to show but im doing something wrong that my eye is not catching. No error messages so kinda lost

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

Attempting to .push() these errror messages to show but im doing something wrong that my eye is not catching. No error messages so kinda lost

问题

I will only translate the non-code part for you:

"尝试进行简单的表单验证,但我始终无法让这些错误消息显示出来。是否有更有经验的人可以帮助我找出我做错了什么,或者是否有更好/更简单的方法来显示这些错误消息... 我正在尝试从if语句中使用.push()将字符串推送出来,如果不满足要求,但我只是看不到任何东西显示出来。可能是因为我如何设置css显示,但我现在完全不确定要查找的位置... 新手尝试掌握基础的JavaScript。"

英文:

trying to do simple form verification and for the life of me cant get these error messages to show up. Could someone with more experience assist me in what I am doing wrong or if there is a better/simpler way to show these error messages ..im trying to .push() the strings from the if state if requirements are not met but i am just not seeing anything show up. Possibly how im doing the css display but im just not entirely sure where to look at this point...New to javascript trying to get the basic down

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

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

let terms = document.getElementById(&#39;terms&#39;)
const username = document.getElementById(&#39;username&#39;)
const password = document.getElementById(&#39;password&#39;)
const form = document.getElementById(&#39;form&#39;)
let errorList = document.getElementById(&#39;error-list&#39;)
let errorContainer = document.getElementById(&#39;error&#39;)
const errorMessages = []
form.addEventListener(&#39;submit&#39;, (e) =&gt; {
  e.preventDefault()

  if (username.value.length &lt; 5) {
    errorMessages.push(&#39;Username must be more than 5 characters&#39;)
  }

  if (password.value.length &lt; 5) {
    errorMessages.push(&#39;Password must be more than 5 characters&#39;)
  }

  if (errorMessages.length &gt; 0) {
    showErrors(errorMessages)
    e.preventDefault()
  }
})

function showErrors(errorMessages) {
  errorMessages.forEach((errorMessage) =&gt; {
    const li = document.createElement(&#39;li&#39;)
    li.innerText = errorMessage
    errorList.appendChild(li)
  })
  errorContainer.classList.add(&#39;show&#39;)
}

<!-- language: lang-css -->

body {
  background-color: silver;
  text-align: center;
  margin-top: 20%;
  color: black;
}

.error {
  display: none;
}

.error .show {
  display: block;
}

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

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot; /&gt;
  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
  &lt;title&gt;Document&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; /&gt;
  &lt;script src=&quot;script.js&quot; defer&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;!--start of the form --&gt;
  &lt;form action=&quot;&quot; id=&quot;form&quot;&gt;
    &lt;!-- Error Container--&gt;
    &lt;div id=&quot;error&quot; class=&quot;error&quot;&gt;
      &lt;ul id=&quot;error-list&quot; class=&quot;error-list&quot;&gt;&lt;/ul&gt;
    &lt;/div&gt;
    &lt;!-- inputs below--&gt;
    &lt;div&gt;
      &lt;input required type=&quot;text&quot; id=&quot;username&quot; /&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;input required type=&quot;password&quot; name=&quot;password&quot; id=&quot;password&quot; /&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;label for=&quot;terms&quot;&gt;Terms &amp; Conditions&lt;/label&gt;
      &lt;input type=&quot;checkbox&quot; name=&quot;terms&quot; id=&quot;terms&quot; /&gt;
    &lt;/div&gt;
    &lt;!--submit button--&gt;
    &lt;button type=&quot;submit&quot; id=&quot;button&quot;&gt;submit&lt;/button&gt;
  &lt;/form&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

.show不是.error的后代,所以不是.error .show(这意味着所有具有show类的元素都位于具有error类的元素内部,将具有该样式,而由于您的错误不位于其内部,所以没有样式)

您需要使用.error.show(这意味着同时具有error和show类的所有元素都将根据您为此选择器指定的规则进行样式设置)

这是您提供的代码的翻译部分。

英文:

.show is not a descendant of .error, so instead of

.error .show (which means that all elements having the show class inside elements having an error class will have that style and since your error is not inside of itself, nothing gets the style)

you will need

.error.show (which means that all elements having the error and the show class at the same time will be styled via the rules you specified for this selector)

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

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

let terms = document.getElementById(&#39;terms&#39;)
const username = document.getElementById(&#39;username&#39;)
const password = document.getElementById(&#39;password&#39;)
const form = document.getElementById(&#39;form&#39;)
let errorList = document.getElementById(&#39;error-list&#39;)
let errorContainer = document.getElementById(&#39;error&#39;)
const errorMessages = []
form.addEventListener(&#39;submit&#39;, (e) =&gt; {
  e.preventDefault()

  if (username.value.length &lt; 5) {
    errorMessages.push(&#39;Username must be more than 5 characters&#39;)
  }

  if (password.value.length &lt; 5) {
    errorMessages.push(&#39;Password must be more than 5 characters&#39;)
  }

  if (errorMessages.length &gt; 0) {
    showErrors(errorMessages)
    e.preventDefault()
  }
})

function showErrors(errorMessages) {
  errorMessages.forEach((errorMessage) =&gt; {
    const li = document.createElement(&#39;li&#39;)
    li.innerText = errorMessage
    errorList.appendChild(li)
  })
  errorContainer.classList.add(&#39;show&#39;)
}

<!-- language: lang-css -->

body {
  background-color: silver;
  text-align: center;
  margin-top: 20%;
  color: black;
}

.error {
  display: none;
}

.error.show {
  display: block;
}

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

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot; /&gt;
  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
  &lt;title&gt;Document&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; /&gt;
  &lt;script src=&quot;script.js&quot; defer&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;!--start of the form --&gt;
  &lt;form action=&quot;&quot; id=&quot;form&quot;&gt;
    &lt;!-- Error Container--&gt;
    &lt;div id=&quot;error&quot; class=&quot;error&quot;&gt;
      &lt;ul id=&quot;error-list&quot; class=&quot;error-list&quot;&gt;&lt;/ul&gt;
    &lt;/div&gt;
    &lt;!-- inputs below--&gt;
    &lt;div&gt;
      &lt;input required type=&quot;text&quot; id=&quot;username&quot; /&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;input required type=&quot;password&quot; name=&quot;password&quot; id=&quot;password&quot; /&gt;
    &lt;/div&gt;
    &lt;div&gt;
      &lt;label for=&quot;terms&quot;&gt;Terms &amp; Conditions&lt;/label&gt;
      &lt;input type=&quot;checkbox&quot; name=&quot;terms&quot; id=&quot;terms&quot; /&gt;
    &lt;/div&gt;
    &lt;!--submit button--&gt;
    &lt;button type=&quot;submit&quot; id=&quot;button&quot;&gt;submit&lt;/button&gt;
  &lt;/form&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月20日 07:54:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059595.html
匿名

发表评论

匿名网友

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

确定