[英]I want to add values into array which i use as property in object data structure in JavaScript
please i want to add some values into an array i have as property in my data structure but i wasn't getting it right.请我想在我的数据结构中作为属性的数组中添加一些值,但我没有做对。 pls i need help.
请问我需要帮助。 bellow is my code.
下面是我的代码。
const Question1 = document.querySelector('.qestion1').textContent;
const Option1 = document.querySelectorAll('input[name="option1"]');
const Question2 = document.querySelector('.qestion2').textContent;
const Option2 = document.querySelectorAll('input[name="option2"]');
const Question3 = document.querySelector('.qestion3').textContent;
const Option3 = document.querySelectorAll('input[name="option3"]');
const Question4 = document.querySelector('.qestion4').textContent;
const Option4 = document.querySelectorAll('input[name="option4"]');
const btn = document.querySelector('.btn');
const questions = {
question1: Question1,
correctAns: 'javaScript',
question2: Question2,
correctAns: 'CSS',
question3: Question3,
correctAns: 'Python',
question4: Question4,
correctAns: 'Html',
choosenAnsers: [],
getAns(Option) {
Option.forEach(function (opt1) {
opt1.addEventListener('click', function (e) {
const chosenAns = e.target.value;
this.choosenAnsers.push(chosenAns);
});
});
},
};
console.log(questions.question1);
questions.getAns(Option1);
here is my Html这是我的 Html
<p class="qestion1">Q1: What is your favorite programming language</p><br />
<input type="radio" name="option1" value="javaScript" id="A1" />
<label for="A1"> A: javaScript</label><br />
<input type="radio" name="option1" value="Python" id="B1" />
<label for="B1"> B: Python</label><br />
<input type="radio" name="option1" value="Rust" id="C1" />
<label for="C1"> C: Rust</label><br />
<input type="radio" name="option1" value="C++" id="D1" />
<label for="D1"> D: C++</label><br />
and there is the error am getting并且出现了错误
Uncaught TypeError: Cannot read properties of undefined (reading 'push') at HTMLInputElement.未捕获的类型错误:无法在 HTMLInputElement 处读取未定义的属性(读取“推送”)。 (questionScript.js:42:28)
(questionScript.js:42:28)
the value of this
inside of an event listener is going to be the HTML element that is the event's currentTarget
, so basically whatever you attached the event listener to.事件侦听器内部的
this
的值将是 HTML 元素,它是事件的currentTarget
,所以基本上无论您将事件侦听器附加到什么。 this
will not be the questions
const as you seem to expect. this
不会是您似乎期望的questions
。
A few more things:还有几件事:
you have multiple object properties all trying to use the same name correctAns
, which will not work.您有多个 object 属性都试图使用相同的名称
correctAns
,这是行不通的。 answers has a 'w' in it.答案中有一个“w”。
As I`ve seen in you examples your JavaScript and your HTML has a lot of typos, and would not work.正如我在您的示例中看到的那样,您的 JavaScript 和 HTML 有很多拼写错误,并且无法正常工作。
The questions
is this snippet is an array, that can be used to have more questions in it, and although I did`nt make the HTML write itself, this should give a concept of how it should work. questions
是这个片段是一个数组,可以用来在其中包含更多问题,虽然我没有让 HTML 自己编写,但这应该给出它应该如何工作的概念。
const questions = [ { question: "Q1: What is your favorite programming language", name: 'question1', options: [ 'JavaScript', 'Python', 'Rust', 'C++' ], correct: 'JavaScript', isCorrect: function () { var radios = document.getElementsByName('question1'); for (let i = 0, length = radios.length; i < length; i++) { if (radios[i].checked && radios[i].value == this.correct) { return true; } } return false; } }, ]; function confirmChoices() { for (let i = 0; i < questions.length; i++) { console.log(questions[i].isCorrect()); } }
<p class="qestion1">Q1: What is your favorite programming language</p> <br /> <input type="radio" name="question1" value="JavaScript" id="A1" /> <label for="A1"> A: JavaScript</label><br /> <input type="radio" name="question1" value="Python" id="B1" /> <label for="B1"> B: Python</label><br /> <input type="radio" name="question1" value="Rust" id="C1" /> <label for="C1"> C: Rust</label><br /> <input type="radio" name="question1" value="C++" id="D1" /> <label for="D1"> D: C++</label><br /> <button onclick="confirmChoices()">Confirm</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.