如何通过使用带有 java 脚本的数组来获取每个值?

我想从数组中获取每个值,当我选择或检查每个值时,我只想获取一个值。但是有一些错误,我不知道如何修复它。我该如何解决它才能正常工作?请告诉我。谢谢


var pzArray = [];


function pizzaOrder() {

  var orderList = {

    pizzaName:"",

    size:"",

    topping:""

  };


  orderList.pizzaName = document.getElementById("pzName").value;

  orderList.size = document.getElementByName("pzSize").value;

  orderList.topping = document.getElementById("topping").checked;


  pzArray.push(orderList);


  for (var i = 0; i<pzArray.length; i++) {

    var pizza = pzArray[i];

    invoice = pizza.size + pizza.pizzaName + pizza.topping;

    totalInvoice += invoice + "<br>"

  }

  document.getElementById("showList").innerHTML = totalInvoice;

}

   <body>


<div>


    <input type=radio id="pzName" name=pzSelect value="A">a

    <input type=radio id="pzName" name=pzSelect value="B">b

    <input type=radio id="pzName" name=pzSelect value="C">c


    <br><br>


    <select>

        <option name="pzSize" id="small" value="Small">S

        <option name="pzSize" id="medium" value="Medium">M

        <option name="pzSize" id="large" value="Large">L

    </select>


    <br><br>


    <input type="checkbox" id="topping" name="ExtraCheese" value="Extra Cheese">XtraC

    <input type="checkbox" id="topping" name="Pepperoni" value="Pepperoni">P

    <input type="checkbox" id="topping" name="Mushrooms" value="Mushrooms">M


    <br><br>


    <input type=button value="Order Description" onClick="pizzaOrder()" />


</div>


<span id="showlist"><span></span>



</body>


holdtom
浏览 108回答 2
2回答

ABOUTYOU

有几个错误没有在你设置的字符串中设置名称,name=pizSelect而不是name="pizSelect"在 ids 中输入错误,没有初始化totalInvoicewith var 或 let 并且在 forloop 中缺少条件 i<pzArray.length。至于单选按钮,你可以做得到document.querySelector("input[name=pzSelect]:checked").value 的价值var pzArray = [];function pizzaOrder() {&nbsp;document.getElementById("showlist").innerHTML&nbsp; var orderList = {&nbsp; &nbsp; pizzaName:"",&nbsp; &nbsp; size:"",&nbsp; &nbsp; topping:""&nbsp; };&nbsp; orderList.pizzaName = document.querySelector("input[name=pzSelect]:checked").value;&nbsp; orderList.size = document.getElementById("psize").value;&nbsp; let toppings=document.querySelectorAll("input[name=topping]:checked");&nbsp; toppings.forEach( (topping)=> orderList.topping+=topping.value+"," ) ;&nbsp; let totalInvoice="";&nbsp; pzArray.push(orderList);&nbsp; for (let i = 0; i< pzArray.length; i++) {&nbsp; &nbsp; let pizza = pzArray[i];&nbsp; &nbsp; invoice = `Order number : ${i+1} ${pizza.size} ${pizza.pizzaName} with ${pizza.topping}`;&nbsp; &nbsp; totalInvoice += invoice + "<br>"&nbsp; }&nbsp; document.getElementById("showlist").innerHTML = totalInvoice;}<h4>Order Pizza</h4><div>&nbsp; &nbsp; <!-- in your code you have not included pzSelect in " " -->&nbsp; &nbsp; <input type=radio id="pzName" name="pzSelect" value="Chicago">Chicago Pizza&nbsp; &nbsp; <input type=radio id="pzName" name="pzSelect" value="Sicilian">Sicilian Pizza&nbsp; &nbsp; <input type=radio id="pzName" name="pzSelect" value="Detroit">DetroitPizza&nbsp; &nbsp; <br><br>&nbsp; &nbsp; Size&nbsp; &nbsp; <select id="psize">&nbsp; &nbsp; &nbsp; &nbsp; <option name="pzSize" id="small" value="Small">Small&nbsp; &nbsp; &nbsp; &nbsp; <option name="pzSize" id="medium" value="Medium">Medium&nbsp; &nbsp; &nbsp; &nbsp; <option name="pzSize" id="large" value="Large">Large&nbsp; &nbsp; </select>&nbsp; &nbsp; <br><br>&nbsp; &nbsp; <input type="checkbox" id="topping" name="topping" value="Extra Cheese">XtraC&nbsp; &nbsp; <input type="checkbox" id="topping" name="topping" value="Pepperoni">Pepporni&nbsp; &nbsp; <input type="checkbox" id="topping" name="topping" value="Mushrooms">Mushrooms&nbsp; &nbsp; <br><br>&nbsp; &nbsp; <input type=button value="Order Description" onClick="pizzaOrder()" /></div><span id="showlist"><span>

吃鸡游戏

正确初始化您的 orderList 对象。首先作为一个空对象:var orderList = {};// then adding the attributesorderList.pizzaName = document.getElementById("pzName").value;orderList.size = document.getElementByName("pzSize").value;orderList.topping = document.getElementById("topping").checked;或直接初始化对象属性var orderList = {&nbsp; &nbsp; pizzaName = document.getElementById("pzName").value,&nbsp; &nbsp; size = document.getElementByName("pzSize").value,&nbsp; &nbsp; topping = document.getElementById("topping").checked}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript