英文:
how to stay hidden the image if the answer still on the input
问题
这是你的代码,我不会翻译它,只会返回原文:
So i have this identification, i hide my image if input value is equal to data-letter
. but i got a problem where i proceed to the next input.. check my code..
was there any logic that can help me with this? I don't want to use jQuery since i'm practicing JavaScript.. TIA
Here's my code
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.querySelectorAll("#choicesIDFExam").forEach((input) => {
input.addEventListener("keyup", function () {
var inptValue = this.value;
document.querySelectorAll("#imglabelIDFExam").forEach((imgLabel) => {
var attribute = imgLabel.getAttribute('data-letter');
if (inptValue == attribute)
{ imgLabel.setAttribute('hidden', true) }
else
{ imgLabel.removeAttribute('hidden'); }
})
})
})
<!-- language: lang-html -->
<div class="__wrapContainerExamAR">
<div class="__imagesContainerIDFExam">
<div class="__lalagyanImage">
<label id="imglabelIDFExam" data-letter="a">
a.
<img src="https://static.thenounproject.com/png/2932881-200.png" />
</label>
<label id="imglabelIDFExam" data-letter="b">
b.
<img src="https://static.thenounproject.com/png/2932881-200.png" />
</label>
<label id="imglabelIDFExam" data-letter="c">
c.
<img src="https://static.thenounproject.com/png/2932881-200.png" />
</label>
</div>
</div>
<div class="__choicesContainerIDFExam" style="display: flex;flex-direction: column;font-size: 24px;">
<label>
<input type="text" id="choicesIDFExam" style="outline:none;border: none;border-bottom: 1px solid;width: 70px;text-align:center" maxlength="1"/>
1. <span id="choicesAExamML" class="__choicesMLExam">Use for controlling input and output devices with the use of programmable memory.</span>
</label>
<label>
<input type="text" id="choicesIDFExam" style="outline:none;border: none;border-bottom: 1px solid;width: 70px;text-align:center" maxlength="1"/>
2. <span id="choicesAExamML" class="__choicesMLExam">Use for joining two pieces of metal by deforming one or both of them to hold one another.</span>
</label>
<label>
<input type="text" id="choicesIDFExam" style="outline:none;border: none;border-bottom: 1px solid;width: 70px;text-align:center" maxlength="1"/>
3. <span id="choicesAExamML" class="__choicesMLExam">It is usually copper or aluminum, and either solid or stranded in construction.</span>
</label>
</div>
</div>
<!-- end snippet -->
英文:
So i have this identification, i hide my image if input value is equal to data-letter
. but i got a problem where i proceed to the next input.. check my code..
was there any logic that can help me with this? I don't want to use jQuery since i'm practicing JavaScript.. TIA
Here's my code
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.querySelectorAll("#choicesIDFExam").forEach((input) => {
input.addEventListener("keyup", function () {
var inptValue = this.value;
document.querySelectorAll("#imglabelIDFExam").forEach((imgLabel) => {
var attribute = imgLabel.getAttribute('data-letter');
if (inptValue == attribute)
{ imgLabel.setAttribute('hidden', true) }
else
{ imgLabel.removeAttribute('hidden'); }
})
})
})
<!-- language: lang-html -->
<div class="__wrapContainerExamAR">
<div class="__imagesContainerIDFExam">
<div class="__lalagyanImage">
<label id="imglabelIDFExam" data-letter="a">
a.
<img src="https://static.thenounproject.com/png/2932881-200.png" />
</label>
<label id="imglabelIDFExam" data-letter="b">
b.
<img src="https://static.thenounproject.com/png/2932881-200.png" />
</label>
<label id="imglabelIDFExam" data-letter="c">
c.
<img src="https://static.thenounproject.com/png/2932881-200.png" />
</label>
</div>
</div>
<div class="__choicesContainerIDFExam" style="display: flex;flex-direction: column;font-size: 24px;">
<label>
<input type="text" id="choicesIDFExam" style="outline:none;border: none;border-bottom: 1px solid;width: 70px;text-align:center" maxlength="1"/>
1. <span id="choicesAExamML" class="__choicesMLExam">Use for controlling input and output devices with the use of programmable memory.</span>
</label>
<label>
<input type="text" id="choicesIDFExam" style="outline:none;border: none;border-bottom: 1px solid;width: 70px;text-align:center" maxlength="1"/>
2. <span id="choicesAExamML" class="__choicesMLExam">Use for joining two pieces of metal by deforming one or both of them to hold one another.</span>
</label>
<label>
<input type="text" id="choicesIDFExam" style="outline:none;border: none;border-bottom: 1px solid;width: 70px;text-align:center" maxlength="1"/>
3. <span id="choicesAExamML" class="__choicesMLExam">It is usually copper or aluminum, and either solid or stranded in construction.</span>
</label>
</div>
</div>
<!-- end snippet -->
答案1
得分: 1
元素ID应该是唯一的,所以你应该使用类名来选择元素。您可以循环遍历元素以检查它们在输入事件中的值。
function getInputValues() {
const inputValues = [];
document.querySelectorAll('.choicesIDFExam').forEach(input => {
inputValues.push(input.value);
});
return inputValues;
}
document.querySelectorAll('.choicesIDFExam').forEach(input => {
input.addEventListener("input", () => {
document.querySelectorAll('.imglabelIDFExam').forEach(imgLabel => {
const attribute = imgLabel.getAttribute('data-letter');
if (getInputValues().includes(attribute)) {
imgLabel.setAttribute('hidden', true);
} else {
imgLabel.removeAttribute('hidden');
}
});
});
});
<div class="__wrapContainerExamAR">
<div class="__imagesContainerIDFExam">
<div class="__lalagyanImage">
<label class="imglabelIDFExam" data-letter="a">
a.
<img src="https://static.thenounproject.com/png/2932881-200.png" />
</label>
<label class="imglabelIDFExam" data-letter="b">
b.
<img src="https://static.thenounproject.com/png/2932881-200.png" />
</label>
<label class="imglabelIDFExam" data-letter="c">
c.
<img src="https://static.thenounproject.com/png/2932881-200.png" />
</label>
</div>
</div>
<div class="__choicesContainerIDFExam" style="display: flex;flex-direction: column;font-size: 24px;">
<label>
<input type="text" class="choicesIDFExam" style="outline:none;border: none;border-bottom: 1px solid;width: 70px;text-align:center" maxlength="1">
1. <span class="__choicesMLExam">用于通过可编程存储器控制输入和输出设备。</span>
</label>
<label>
<input type="text" class="choicesIDFExam" style="outline:none;border: none;border-bottom: 1px solid;width: 70px;text-align:center" maxlength="1">
2. <span class="__choicesMLExam">用于通过变形金属的一方或双方来连接两块金属。</span>
</label>
<label>
<input type="text" class="choicesIDFExam" style="outline:none;border: none;border-bottom: 1px solid;width: 70px;text-align:center" maxlength="1">
3. <span class="__choicesMLExam">通常是铜或铝,构造可以是实心或多股绞线。</span>
</label>
</div>
</div>
英文:
Element IDs should be unique, so you should use class names to select the elements instead. You can loop through the elements to check their values within the input event.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function getInputValues() {
const inputValues = [];
document.querySelectorAll('.choicesIDFExam').forEach(input => {
inputValues.push(input.value);
});
return inputValues;
}
document.querySelectorAll('.choicesIDFExam').forEach(input => {
input.addEventListener("input", () => {
document.querySelectorAll('.imglabelIDFExam').forEach(imgLabel => {
const attribute = imgLabel.getAttribute('data-letter');
if (getInputValues().includes(attribute)) {
imgLabel.setAttribute('hidden', true);
} else {
imgLabel.removeAttribute('hidden');
}
});
});
});
<!-- language: lang-html -->
<div class="__wrapContainerExamAR">
<div class="__imagesContainerIDFExam">
<div class="__lalagyanImage">
<label class="imglabelIDFExam" data-letter="a">
a.
<img src="https://static.thenounproject.com/png/2932881-200.png" />
</label>
<label class="imglabelIDFExam" data-letter="b">
b.
<img src="https://static.thenounproject.com/png/2932881-200.png" />
</label>
<label class="imglabelIDFExam" data-letter="c">
c.
<img src="https://static.thenounproject.com/png/2932881-200.png" />
</label>
</div>
</div>
<div class="__choicesContainerIDFExam" style="display: flex;flex-direction: column;font-size: 24px;">
<label>
<input type="text" class="choicesIDFExam" style="outline:none;border: none;border-bottom: 1px solid;width: 70px;text-align:center" maxlength="1"/>
1. <span class="__choicesMLExam">Use for controlling input and output devices with the use of programmable memory.</span>
</label>
<label>
<input type="text" class="choicesIDFExam" style="outline:none;border: none;border-bottom: 1px solid;width: 70px;text-align:center" maxlength="1"/>
2. <span class="__choicesMLExam">Use for joining two pieces of metal by deforming one or both of them to hold one another.</span>
</label>
<label>
<input type="text" class="choicesIDFExam" style="outline:none;border: none;border-bottom: 1px solid;width: 70px;text-align:center" maxlength="1"/>
3. <span class="__choicesMLExam">It is usually copper or aluminum, and either solid or stranded in construction.</span>
</label>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论