英文:
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('.js-input').value;
const button = document.querySelector('.js-button');
const body = document.querySelector('body');
button.addEventListener('click', yearfunc);
function yearfunc(event) {
if (input == 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();
}
<!-- language: lang-html -->
<p>What the year is it right now?</p><br><br>
<input type="text" class="js-input">
<button class="js-button">Send</button>
<!-- 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(".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();
}
<!-- language: lang-html -->
<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>
<!-- 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('.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 = 'Correct';
}
else {
const div = document.createElement('div');
button.insertAdjacentElement('afterend', div);
div.innerText = 'Incorrect';
}
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("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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论