简体   繁体   English

我想将值添加到我用作 JavaScript 中 object 数据结构中的属性的数组中

[英]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.

相关问题 我想从数组中的 object 获取值键 - I want to get values key from object which is in array Javascript:如何将数组值添加到对象属性 - Javascript: How to add array values to object property 想要更改复杂的 Json 结构,我需要在 Javascript 中更改它的数组 - Want to change complex Json structure which I need change it array in Javascript 如何将 JavaScript object 的属性值提取到数组中? - How might I extract the property values of a JavaScript object into an array? 我想在CSS属性中使用Javascript函数 - I want to use a Javascript function in a css property JS REDUX STORE 我想将 object 添加到对象数组中,该对象数组嵌套在数组中的特定 object 中 - JS REDUX STORE I want to add an object to an array of objects, which is nest in a specific object in an array 想要使用 object 数组中的数组键作为新属性 javascript - want to use array key in array of object as new property javascript 我应该使用哪种方法来比较对象数组中的值? Javascript - Which method should I use to compare values in array of objects? Javascript 我想在 JAVASCRIPT 中将字符串(包含浮点值数组)转换为浮点数组 - I want to convert string ( which contain array of float values ) into float array in JAVASCRIPT 我想将数组值转换为单个对象 - I want to convert array values to single object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM