简体   繁体   English

如何正确推入 JavaScript 中的空数组?

[英]How do you correctly push into Empty Array in JavaScript?

Hey I am creating a simple Star Wars quiz and could use some help.嘿,我正在创建一个简单的星球大战测验,可以使用一些帮助。 Thank you!谢谢!

JS(Idea here is to get values to later push into array) JS(这里的想法是获取值以稍后推送到数组中)

function addValues() {
  let value1 = document.getElementById('Q1').value;
  let value2 = document.getElementById('Q2').value;
  let value3 = document.getElementById('Q3').value;

  answerChoices.push(value1);
  answerChoices.push(value2);
  answerChoices.push(value3);

  console.log(value1);
}

HTML (Added Html to replace index when start button is clicked) HTML(添加 Html 以在单击开始按钮时替换索引)


<p id='timer'>Timer</p>
<div id='questions'>

  <h3>Where did Luke Skywalker Grow Up?</h3>
  <form id="Q1">
    <input type="radio" name="birthPlanet" id="Hoth" value="Hoth" /><label>Hoth</label>
    <input type="radio" name="birthPlanet" id="Tatooine" value="Tatooine" /><label>Tatooine</label>
    <input type="radio" name="birthPlanet" id="Endor" value="Endor" /><label>Endor</label>
  </form>

  <h3>What planet did the Death Star blow up?</h3>
  <form id="Q2">
    <input type="radio" name="Q2" id="Q2" value="Exogal" /><label>Exogal</label>
    <input type="radio" name="Q2" id="Q2" value="Yavin 4" /><label>Yavin 4</label>
    <input type="radio" name="Q2" id="Q2" value="Aldreaan" /><label>Aldreaan</label>
  </form>

  <h3>What year did Star War first come out?</h3>
  <form id="Q3">
    <input type="text" name="Q3" id="Q3" /><label>Enter Text Here</label>
  </form> <br />
  <button onclick="endButton()">Finish Quiz</button>
</div>

ARRAY let answerChoices= []; ARRAY let answerChoices= [];

May be this is what you want.. try snippet可能这就是你想要的.. 尝试片段

 function addValues(){ let value1 = document.querySelector('input[name="Q1"]:checked'); let value2 = document.querySelector('input[name="Q2"]:checked'); let value3 = document.querySelector('input[name="Q3"]'); let answerChoices = { 'Q1': value1.value, 'Q2': value2.value, 'Q3': value3.value } // console the hole values console.log(answerChoices); // console single value console.log(answerChoices.Q1); }
 <p id= 'timer'>Timer</p> <div id='questions'> <h3>Where did Luke Skywalker Grow Up?</h3> <form id="Q1"> <input type="radio" name="Q1" id="Hoth" value="Hoth" /><label>Hoth</label> <input type="radio" name="Q1" id="Tatooine" value="Tatooine" /><label>Tatooine</label> <input type="radio" name="Q1" id="Endor" value="Endor" /><label>Endor</label> </form> <h3>What planet did the Death Star blow up?</h3> <form id="Q2"> <input type="radio" name="Q2" id="Q2" value="Exogal" /><label>Exogal</label> <input type="radio" name="Q2" id="Q2" value="Yavin 4" /><label>Yavin 4</label> <input type="radio" name="Q2" id="Q2" value="Aldreaan" /><label>Aldreaan</label> </form> <h3>What year did Star War first come out?</h3> <form id="Q3"> <input type="text" name="Q3" id="Q3" /><label>Enter Text Here</label> </form> <br/> <button onclick="addValues()">Finish Quiz</button> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM