代码中什么都没有发生,尽管它可能是正确的。

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

Nothing happens in my code though it could be correct

问题

    <p>现在是什么年份?</p><br><br>
    <input type="text" class="js-input">
    <button class="js-button">发送</button>
英文:

I've no clue what should I add or change in my code. This program is considered to output 'correct' when you text in current year '2023' but I don't get any errors or anything else. It just doesn't work.
P.S it outputs 'incorrect' even if it's true

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

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

const date = new Date();
const input = document.querySelector(&#39;.js-input&#39;).value;
const button = document.querySelector(&#39;.js-button&#39;);
const body = document.querySelector(&#39;body&#39;);

button.addEventListener(&#39;click&#39;, yearfunc);

function yearfunc(event) {
  if (input == date.getFullYear()) {
    const div = document.createElement(&#39;div&#39;);
    button.insertAdjacentElement(&#39;afterend&#39;, div);
    div.innerText = &#39;Correct&#39;;
  } else {
    const div = document.createElement(&#39;div&#39;);
    button.insertAdjacentElement(&#39;afterend&#39;, div);
    div.innerText = &#39;Incorrect&#39;;
  }
  event.preventDefault();
}

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

&lt;p&gt;What the year is it right now?&lt;/p&gt;&lt;br&gt;&lt;br&gt;
&lt;input type=&quot;text&quot; class=&quot;js-input&quot;&gt;
&lt;button class=&quot;js-button&quot;&gt;Send&lt;/button&gt;

<!-- end snippet -->

答案1

得分: 4

The difference is that you assigned '.value' too early in the code.
Assigning it directly in the function and comparing using a new variable solves the problem, try this out:

const date = new Date();
const input = document.querySelector(".js-input");
const button = document.querySelector(".js-button");

button.addEventListener("click", yearfunc);

function yearfunc(event) {
  const value = input.value;
  console.log(`VALUE: `, value);
  console.log(`YEAR: `, date.getFullYear());
  if (value == date.getFullYear()) {
    const div = document.createElement("div");
    button.insertAdjacentElement("afterend", div);
    div.innerText = "Correct";
  } else {
    const div = document.createElement("div");
    button.insertAdjacentElement("afterend", div);
    div.innerText = "Incorrect";
  }

  event.preventDefault();
}
<p>What year is it right now?</p>
<br /><br />
<input type="number" class="js-input" />
<button class="js-button">Send</button>

<script src="javascript.js"></script>
英文:

The difference is that you assigned '.value' too early in the code.
Assigning it directly in the function and comparing using a new variable solves the problem, try this out:

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

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

const date = new Date();
const input = document.querySelector(&quot;.js-input&quot;);
const button = document.querySelector(&quot;.js-button&quot;);

button.addEventListener(&quot;click&quot;, yearfunc);

function yearfunc(event) {
  const value = input.value;
  console.log(`VALUE: `, value);
  console.log(`YEAR: `, date.getFullYear());
  if (value == date.getFullYear()) {
    const div = document.createElement(&quot;div&quot;);
    button.insertAdjacentElement(&quot;afterend&quot;, div);
    div.innerText = &quot;Correct&quot;;
  } else {
    const div = document.createElement(&quot;div&quot;);
    button.insertAdjacentElement(&quot;afterend&quot;, div);
    div.innerText = &quot;Incorrect&quot;;
  }

  event.preventDefault();
}

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

&lt;p&gt;What year is it right now?&lt;/p&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;input type=&quot;number&quot; class=&quot;js-input&quot; /&gt;
&lt;button class=&quot;js-button&quot;&gt;Send&lt;/button&gt;

&lt;script src=&quot;javascript.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

答案2

得分: 2

你在事件外部检索输入值,所以当事件实际发生时,你只是在比较一个预加载的值" "。解决方法是在事件处理程序内部获取输入数据:

// JavaScript 代码
const button = document.querySelector('.js-button');
const body = document.querySelector('body');
button.addEventListener('click', yearfunc);

function yearfunc(event) {
  const date = new Date();
  const input = document.querySelector('.js-input').value;
  if (input == date.getFullYear()) {
    const div = document.createElement('div');
    button.insertAdjacentElement('afterend', div);
    div.innerText = '正确';
  } else {
    const div = document.createElement('div');
    button.insertAdjacentElement('afterend', div);
    div.innerText = '错误';
  }

  event.preventDefault();
}
英文:

you Are retrieving the input outside of event so when the event actually occurs you are just comparing a pre-loaded value of "". Solution is to fetch the input data inside event handler:

// JavaScript code
const button = document.querySelector(&#39;.js-button&#39;);
const body = document.querySelector(&#39;body&#39;);
button.addEventListener(&#39;click&#39;, yearfunc);

function yearfunc(event) {
  const date = new Date();
  const input = document.querySelector(&#39;.js-input&#39;).value; 
    if(input == date.getFullYear()) {
        const div = document.createElement(&#39;div&#39;);
        button.insertAdjacentElement(&#39;afterend&#39;, div);
        div.innerText = &#39;Correct&#39;;
    }

    else {
        const div = document.createElement(&#39;div&#39;);
        button.insertAdjacentElement(&#39;afterend&#39;, div);
        div.innerText = &#39;Incorrect&#39;;
    }

    event.preventDefault();
}

答案3

得分: 0

这段代码应该可以工作。从'js-input'类中获取值的方式并没有提供正确的输入值。

const date = new Date();
const input1 = document.getElementsByClassName("js-input");
const button = document.querySelector('.js-button');
const body = document.querySelector('body');

button.addEventListener('click', yearfunc);

function yearfunc(event) {
  console.log("in the function")
  console.log(input1[0].value)
  console.log(date.getFullYear())
  if(input1[0].value == String(date.getFullYear())) {
    const div = document.createElement('div');
    button.insertAdjacentElement('afterend', div);
    div.innerText = 'Correct';
  }

  else {
    const div = document.createElement('div');
    button.insertAdjacentElement('afterend', div);
    div.innerText = 'Incorrect';
  }

  event.preventDefault();
}
英文:

This code should work. The way value was being taken from the 'js-input' class, was not giving the correct input value.

const date = new Date();
const input1 = document.getElementsByClassName(&quot;js-input&quot;);
const button = document.querySelector(&#39;.js-button&#39;);
const body = document.querySelector(&#39;body&#39;);

button.addEventListener(&#39;click&#39;, yearfunc);

function yearfunc(event) {
  console.log(&quot;in the function&quot;)
  console.log(input1[0].value)
  console.log(date.getFullYear())
    if(input1[0].value == String(date.getFullYear())) {
        const div = document.createElement(&#39;div&#39;);
        button.insertAdjacentElement(&#39;afterend&#39;, div);
        div.innerText = &#39;Correct&#39;;
    }

    else {
        const div = document.createElement(&#39;div&#39;);
        button.insertAdjacentElement(&#39;afterend&#39;, div);
        div.innerText = &#39;Incorrect&#39;;
    }

  event.preventDefault();
}

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

发表评论

匿名网友

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

确定