js code runs and then refreshes quickly i cannot notice the changes more than a second after running the code

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

js code runs and then refreshes quickly i cannot notice the changes more than a second after running the code

问题

这是您提供的代码的翻译:

let requestBtn = document.getElementById("requestbtn")
let oops = document.querySelector(".oops")

requestBtn.addEventListener("click", () => {
  let emailInput = document.getElementById("email").value
  emailInput == "" ? oops.classList.remove("hidden") : null
})
:root {
  font-size: 10px;
  --font-heading: 5.2em;
  --font-paragraph: 18px;
  --green-color: #54E6AF;
  --form-color: #2C344B;
  --background-navy-blue-color: #121725;
  --white: #ffffff;
  --form-hover-color: #5A668A;
  --grey-color: #C2CBE5;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Chivo', sans-serif;
  font-size: 100%;
}

body {
  position: relative;
  width: 100%;
  height: 100vh;
  padding-bottom: 130px;
  background-color: var(--background-navy-blue-color);
}

body::before {
  width: 100%;
  min-height: 100vh;
  position: absolute;
  content: '';
  background-image: url(../assets/desktop/bg-pattern-dots.svg), url(../assets/desktop/image-host.jpg);
  background-repeat: no-repeat;
  background-position: right bottom 10%, right center;
}

body::after {
  width: 100%;
  min-height: 100vh;
  position: absolute;
  content: '';
  background-image: url(../assets/desktop/logo.svg);
  background-repeat: no-repeat;
  background-position: left 20% top 10%;
}

main {
  z-index: 1;
  position: absolute;
  top: 50%;
  left: 10%;
  transform: translateY(-40%);
  max-width: fit-content;
  padding: 90px 60px 0 0;
  background-color: var(--background-navy-blue-color);
}

.container {
  display: flex;
  flex-direction: column;
}

main h1 {
  font-size: 5.2em;
  font-weight: 400;
  line-height: 62px;
  text-transform: uppercase;
  color: var(--green-color);
}

main h1 span {
  color: var(--grey-color);
}

main p {
  max-width: 445px;
  font-size: 18px;
  font-weight: 400;
  line-height: 28px;
  color: var(--grey-color);
  opacity: 0.5;
  margin-top: 24px;
}

form {
  margin-top: 40px;
  position: relative;
  width: 430px;
}

input {
  width: 100%;
  padding: 20px 0 20px 32px;
  background-color: var(--form-color);
  border: none;
  outline: none;
  border-radius: 1000px;
  font-size: 14px;
  font-weight: 400;
  color: white;
}

input::placeholder {
  color: white;
  font-size: 14px;
  font-weight: 400;
}

button {
  color: black;
  height: 80%;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  right: 8px;
  padding: 10px 28px;
  border-radius: 1000px;
  border: none;
  cursor: pointer;
  background-color: var(--green-color);
  font-size: 14px;
  font-weight: 400;
}

button:hover {
  background-color: #B3FFE2;
}

.img-contianer {
  margin-top: 42px;
  display: flex;
  gap: 40px;
}

.img-contianer img {
  object-fit: contain;
}

.oops {
  color: #FB3E3E;
  opacity: 1;
  margin: 0;
}

.hidden {
  opacity: 0;
}

@media (max-width: 1250px) {
  body::after {
    background-size: 200px;
    background-position: left 40px top 50px;
  }
  body::before {
    background-image: url(../assets/desktop/bg-pattern-dots.svg), url(../assets/tablet/image-host.jpg);
    background-position: left 5% bottom, right top;
  }
  main {
    left: 0;
  }
  main h1 {
    font-size: 48px;
  }
}

@media (max-width: 800px) {
  body::after {
    background-position: center top 10%;
    opacity: 1;
  }
  body::before {
    background-image: url(../assets/mobile/image-host.jpg);
    background-position: top center;
    background-size: cover;
    backface-visibility: hidden;
    opacity: 0.1;
  }
  main {
    width: 100%;
    max-width: 100%;
    padding: 0 24px;
    background-color: transparent;
    bottom: 0;
  }
  main h1 {
    text-align: center;
  }
  main p {
    text-align: center;
  }
  .container {
    align-items: center;
  }
  .img-contianer {
    order: 2;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: center;
    opacity: 0.8;
    margin-top: 30px;
  }
  form {
    order: 3;
  }
  button {
    width: 100%;
    position: static;
    padding: 20px 0;
    margin-top: 16px;
    transform: translate(0, 0);
  }
}
<main>
  <h1>Publish your podcasts<br><span>everywhere</span>.</h1>
  <div class="container">
    <p>Upload your audio to Pod with a single click. We’ll then distribute your podcast to Spotify, Apple Podcasts, Google Podcasts, Pocket Casts and more!</p>
    <form>
      <input type="email" placeholder="Email address" id="email">
      <button id="requestbtn">Request Access</button>
    </form>
    <p class="oops hidden">Oops! Please check your email</p>
    <div class="img-contianer">
      <img src="assets/desktop/spotify.svg" alt="spotify">
      <img src="assets/desktop/apple-podcast.svg" alt="apple-podcast">
      <img src="assets/desktop/google-podcasts.svg" alt="google">
      <img src="assets/desktop/pocket-casts.svg" alt="pocket">
    </div>
  </div>
</main>

这是您提供的代码的翻译。如果您需要进一步的帮助或解释,请随时告诉我。

英文:

https://github.com/Ahmedalzarka/pod-request-access-landing-page:

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

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

let requestBtn = document.getElementById(&quot;requestbtn&quot;)
let oops = document.querySelector(&quot;.oops&quot;)
requestBtn.addEventListener(&quot;click&quot;, () =&gt; {
let emailInput = document.getElementById(&quot;email&quot;).value
emailInput == &quot;&quot; ? oops.classList.remove(&quot;hidden&quot;) : null
})

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

:root {
font-size: 10px;
--font-heading: 5.2em;
--font-paragraph: 18px;
--green-color: #54E6AF;
--form-color: #2C344B;
--background-navy-blue-color: #121725;
--white: #ffffff;
--form-hover-color: #5A668A;
--grey-color: #C2CBE5;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: &#39;Chivo&#39;, sans-serif;
font-size: 100%;
}
body {
position: relative;
width: 100%;
height: 100vh;
padding-bottom: 130px;
background-color: var(--background-navy-blue-color);
}
body::before {
width: 100%;
min-height: 100vh;
position: absolute;
content: &#39;&#39;;
background-image: url(../assets/desktop/bg-pattern-dots.svg), url(../assets/desktop/image-host.jpg);
background-repeat: no-repeat;
background-position: right bottom 10%, right center;
}
body::after {
width: 100%;
min-height: 100vh;
position: absolute;
content: &#39;&#39;;
background-image: url(../assets/desktop/logo.svg);
background-repeat: no-repeat;
background-position: left 20% top 10%;
}
main {
z-index: 1;
position: absolute;
top: 50%;
left: 10%;
transform: translateY(-40%);
max-width: fit-content;
padding: 90px 60px 0 0;
background-color: var(--background-navy-blue-color);
}
.container {
display: flex;
flex-direction: column;
}
main h1 {
font-size: 5.2em;
font-weight: 400;
line-height: 62px;
text-transform: uppercase;
color: var(--green-color);
}
main h1 span {
color: var(--grey-color);
}
main p {
max-width: 445px;
font-size: 18px;
font-weight: 400;
line-height: 28px;
color: var(--grey-color);
opacity: 0.5;
margin-top: 24px;
}
form {
margin-top: 40px;
position: relative;
width: 430px;
}
input {
width: 100%;
padding: 20px 0 20px 32px;
background-color: var(--form-color);
border: none;
outline: none;
border-radius: 1000px;
font-size: 14px;
font-weight: 400;
color: white;
}
input::placeholder {
color: white;
font-size: 14px;
font-weight: 400;
}
button {
color: black;
height: 80%;
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 8px;
padding: 10px 28px;
border-radius: 1000px;
border: none;
cursor: pointer;
background-color: var(--green-color);
font-size: 14px;
font-weight: 400;
}
button:hover {
background-color: #B3FFE2;
}
.img-contianer {
margin-top: 42px;
display: flex;
gap: 40px;
}
.img-contianer img {
object-fit: contain;
}
.oops {
color: #FB3E3E;
opacity: 1;
margin: 0;
}
.hidden {
opacity: 0;
}
@media (max-width: 1250px) {
body::after {
background-size: 200px;
background-position: left 40px top 50px;
}
body::before {
background-image: url(../assets/desktop/bg-pattern-dots.svg), url(../assets/tablet/image-host.jpg);
background-position: left 5% bottom, right top;
}
main {
left: 0;
}
main h1 {
font-size: 48px;
}
}
@media (max-width: 800px) {
body::after {
background-position: center top 10%;
opacity: 1;
}
body::before {
background-image: url(../assets/mobile/image-host.jpg);
background-position: top center;
background-size: cover;
backface-visibility: hidden;
opacity: 0.1;
}
main {
width: 100%;
max-width: 100%;
padding: 0 24px;
background-color: transparent;
bottom: 0;
}
main h1 {
text-align: center;
}
main p {
text-align: center;
}
.container {
align-items: center;
}
.img-contianer {
order: 2;
gap: 10px;
flex-wrap: wrap;
justify-content: center;
opacity: 0.8;
margin-top: 30px;
}
form {
order: 3;
}
button {
width: 100%;
position: static;
padding: 20px 0;
margin-top: 16px;
transform: translate(0, 0);
}
}

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

&lt;main&gt;
&lt;h1&gt;Publish your podcasts&lt;br&gt;&lt;span&gt;everywhere&lt;/span&gt;.&lt;/h1&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;p&gt;Upload your audio to Pod with a single click. We’ll then distribute your podcast to Spotify, Apple Podcasts, Google Podcasts, Pocket Casts and more!&lt;/p&gt;
&lt;form&gt;
&lt;input type=&quot;email&quot; placeholder=&quot;Email address&quot; id=&quot;email&quot;&gt;
&lt;button id=&quot;requestbtn&quot;&gt;Request Access&lt;/button&gt;
&lt;/form&gt;
&lt;p class=&quot;oops hidden&quot;&gt;Oops! Please check your email&lt;/p&gt;
&lt;div class=&quot;img-contianer&quot;&gt;
&lt;img src=&quot;assets/desktop/spotify.svg&quot; alt=&quot;spotify&quot;&gt;
&lt;img src=&quot;assets/desktop/apple-podcast.svg&quot; alt=&quot;apple-podcast&quot;&gt;
&lt;img src=&quot;assets/desktop/google-podcasts.svg&quot; alt=&quot;google&quot;&gt;
&lt;img src=&quot;assets/desktop/pocket-casts.svg&quot; alt=&quot;pocket&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/main&gt;

<!-- end snippet -->

js code runs and then refreshes quickly i cannot notice the changes more than a second after running the code

js code runs and then refreshes quickly i cannot notice the changes more than a second after running the code

The hidden message "oops....." shows and hides in the same second.

I don't know what to do.

答案1

得分: 1

  1. 缺少"type"属性的按钮在点击时不会知道提交表单。
<button type="submit" id="requestbtn">请求访问</button>
  1. 为了考虑按钮按下或用户在电子邮件字段上按下回车键,建议添加事件监听器到表单。

  2. 为了在出现错误时阻止表单提交,请使用 event.preventDefault()

const requestAccessForm = document.getElementById("requestAccessForm");
let requestBtn = document.getElementById("requestbtn");
let oops = document.querySelector(".oops")

requestAccessForm.addEventListener("submit", (event) => {
  let emailInput = document.getElementById("email");
  
  if (emailInput.value === "") {
    oops.classList.remove("hidden");
    
    console.log("无效的电子邮件");
    // 将阻止表单提交
    event.preventDefault();
    
  } else {
    // 用于测试的else语句
    console.log("表单将提交");
  }
});
英文:
  1. The button missing the type attribute won't know to submit the form when button is clicked.
&lt;button type=&quot;submit&quot; id=&quot;requestbtn&quot;&gt;Request Access&lt;/button&gt;
  1. To account for button pressed or user pressing enter on the email field, suggestion to add event listener to form.

  2. To prevent form submission on error, use event.preventDefault()

const requestAccessForm = document.getElementById(&quot;requestAccessForm&quot;);
let requestBtn = document.getElementById(&quot;requestbtn&quot;);
let oops = document.querySelector(&quot;.oops&quot;)


requestAccessForm.addEventListener(&quot;submit&quot;, (event) =&gt; {
  let emailInput = document.getElementById(&quot;email&quot;);
  
  if (emailInput.value === &quot;&quot;) {
    oops.classList.remove(&quot;hidden&quot;);
    
    console.log(&quot;invalid email&quot;);
    // Will prevent form from submitting
    event.preventDefault();
    
  } else {
    // else statement just for testing
    console.log(&quot;form will submit&quot;);
  }
});

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

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

const requestAccessForm = document.getElementById(&quot;requestAccessForm&quot;);
let requestBtn = document.getElementById(&quot;requestbtn&quot;);
let oops = document.querySelector(&quot;.oops&quot;)
requestAccessForm.addEventListener(&quot;submit&quot;, (event) =&gt; {
let emailInput = document.getElementById(&quot;email&quot;);
if (emailInput.value === &quot;&quot;) {
oops.classList.remove(&quot;hidden&quot;);
console.log(&quot;invalid email&quot;);
// Will prevent form from submitting
event.preventDefault();
} else {
// else statement just for testing
console.log(&quot;form will submit&quot;);
}
});

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

:root {
font-size: 10px;
--font-heading: 5.2em;
--font-paragraph: 18px;
--green-color: #54E6AF;
--form-color: #2C344B;
--background-navy-blue-color: #121725;
--white: #ffffff;
--form-hover-color: #5A668A;
--grey-color: #C2CBE5;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: &#39;Chivo&#39;, sans-serif;
font-size: 100%;
}
body {
position: relative;
width: 100%;
height: 100vh;
padding-bottom: 130px;
background-color: var(--background-navy-blue-color);
}
body::before {
width: 100%;
min-height: 100vh;
position: absolute;
content: &#39;&#39;;
background-image: url(../assets/desktop/bg-pattern-dots.svg), url(../assets/desktop/image-host.jpg);
background-repeat: no-repeat;
background-position: right bottom 10%, right center;
}
body::after {
width: 100%;
min-height: 100vh;
position: absolute;
content: &#39;&#39;;
background-image: url(../assets/desktop/logo.svg);
background-repeat: no-repeat;
background-position: left 20% top 10%;
}
main {
z-index: 1;
position: absolute;
top: 50%;
left: 10%;
transform: translateY(-40%);
max-width: fit-content;
padding: 90px 60px 0 0;
background-color: var(--background-navy-blue-color);
}
.container {
display: flex;
flex-direction: column;
}
main h1 {
font-size: 5.2em;
font-weight: 400;
line-height: 62px;
text-transform: uppercase;
color: var(--green-color);
}
main h1 span {
color: var(--grey-color);
}
main p {
max-width: 445px;
font-size: 18px;
font-weight: 400;
line-height: 28px;
color: var(--grey-color);
opacity: 0.5;
margin-top: 24px;
}
form {
margin-top: 40px;
position: relative;
width: 430px;
}
input {
width: 100%;
padding: 20px 0 20px 32px;
background-color: var(--form-color);
border: none;
outline: none;
border-radius: 1000px;
font-size: 14px;
font-weight: 400;
color: white;
}
input::placeholder {
color: white;
font-size: 14px;
font-weight: 400;
}
button {
color: black;
height: 80%;
position: absolute;
top: 50%;
transform: translateY(-50%);
right: 8px;
padding: 10px 28px;
border-radius: 1000px;
border: none;
cursor: pointer;
background-color: var(--green-color);
font-size: 14px;
font-weight: 400;
}
button:hover {
background-color: #B3FFE2;
}
.img-contianer {
margin-top: 42px;
display: flex;
gap: 40px;
}
.img-contianer img {
object-fit: contain;
}
.oops {
color: #FB3E3E;
opacity: 1;
margin: 0;
}
.hidden {
opacity: 0;
}
@media (max-width: 1250px) {
body::after {
background-size: 200px;
background-position: left 40px top 50px;
}
body::before {
background-image: url(../assets/desktop/bg-pattern-dots.svg), url(../assets/tablet/image-host.jpg);
background-position: left 5% bottom, right top;
}
main {
left: 0;
}
main h1 {
font-size: 48px;
}
}
@media (max-width: 800px) {
body::after {
background-position: center top 10%;
opacity: 1;
}
body::before {
background-image: url(../assets/mobile/image-host.jpg);
background-position: top center;
background-size: cover;
backface-visibility: hidden;
opacity: 0.1;
}
main {
width: 100%;
max-width: 100%;
padding: 0 24px;
background-color: transparent;
bottom: 0;
}
main h1 {
text-align: center;
}
main p {
text-align: center;
}
.container {
align-items: center;
}
.img-contianer {
order: 2;
gap: 10px;
flex-wrap: wrap;
justify-content: center;
opacity: 0.8;
margin-top: 30px;
}
form {
order: 3;
}
button {
width: 100%;
position: static;
padding: 20px 0;
margin-top: 16px;
transform: translate(0, 0);
}
}

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

&lt;main&gt;
&lt;h1&gt;Publish your podcasts&lt;br&gt;&lt;span&gt;everywhere&lt;/span&gt;.&lt;/h1&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;p&gt;Upload your audio to Pod with a single click. We’ll then distribute your podcast to Spotify, Apple Podcasts, Google Podcasts, Pocket Casts and more!&lt;/p&gt;
&lt;form id=&quot;requestAccessForm&quot;&gt;
&lt;input type=&quot;email&quot; placeholder=&quot;Email address&quot; id=&quot;email&quot;&gt;
&lt;button type=&quot;submit&quot; id=&quot;requestbtn&quot;&gt;Request Access&lt;/button&gt;
&lt;/form&gt;
&lt;p class=&quot;oops hidden&quot;&gt;Oops! Please check your email&lt;/p&gt;
&lt;div class=&quot;img-contianer&quot;&gt;
&lt;img src=&quot;assets/desktop/spotify.svg&quot; alt=&quot;spotify&quot;&gt;
&lt;img src=&quot;assets/desktop/apple-podcast.svg&quot; alt=&quot;apple-podcast&quot;&gt;
&lt;img src=&quot;assets/desktop/google-podcasts.svg&quot; alt=&quot;google&quot;&gt;
&lt;img src=&quot;assets/desktop/pocket-casts.svg&quot; alt=&quot;pocket&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/main&gt;

<!-- end snippet -->

答案2

得分: 0

当您使用表单标签时,需要注意按钮,因为默认情况下,表单中的按钮类型是 "submit",这会导致每次单击它们时页面都会刷新。

要解决这个问题,只需指定按钮类型,如下所示:

<button type="button" id="requestbtn">请求访问</button>

希望对您有帮助 js code runs and then refreshes quickly i cannot notice the changes more than a second after running the code

英文:

When you are using a form tag, u need to be careful to the buttons, cause by default in a form they are type=&quot;submit&quot;, and this cause the page to refresh every time they are clicked.

To fix the problem just specify the button type like so:

&lt;button type=&quot;button&quot; id=&quot;requestbtn&quot;&gt;Request Access&lt;/button&gt;

Hope it helps js code runs and then refreshes quickly i cannot notice the changes more than a second after running the code

答案3

得分: 0

根据Edoardo Balducci的提到,按钮默认为type="submit"。这意味着单击按钮将尝试提交其关联的表单。

由于输入字段不是required,它是可选的。因为它是表单的唯一表单控件,表单始终有效,将始终在请求时提交(在这里:单击按钮)。

要防止提交,请尝试以下方法之一:

  • 使输入字段为required
  • 有条件地防止点击提交表单(通过调用event.preventDefault())。
  • 将按钮更改为type="button",并有条件地手动提交表单(例如通过调用form.requestSubmit())。

根据Arty.Simon的提到,最好监听表单上的submit事件。这更好地传达了意图,也处理了来自按钮以外的地方发起的表单提交请求。

英文:

As Edoardo Balducci mentions, buttons are type=&quot;submit&quot; by default. This means, clicking the button will try to submit its associated form.

Since the input field is not required, it is optional. Because it is the form's only form control, the form is always valid and will always submit when requested (here: clicking the button).

To prevent submission, try (one of) the following:

  • Make the input field required.
  • Conditionally prevent the click from submitting the form (by calling event.preventDefault()).
  • Change the button to type=&quot;button&quot;, and conditionally submit the form manually (e.g. by calling form.requestSubmit())

As Arty.Simon mentions, it would be better to listen to a submit event on the form. This better conveys intent and also handles form submission requests originating from elsewhere than the button.

huangapple
  • 本文由 发表于 2023年5月7日 21:05:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194112.html
匿名

发表评论

匿名网友

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

确定