亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過java腳本使用數組來獲取每個值?

如何通過java腳本使用數組來獲取每個值?

慕村225694 2023-10-17 16:58:03
我想從數組中獲取每個值,當我選擇或檢查每個值時,我只想獲取一個值。但有一些錯誤,我不知道如何修復它。我該如何修復它才能正常工作?請告訴我。謝謝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>
查看完整描述

2 回答

?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

有幾個錯誤沒有在您設置的字符串中設置名稱,name=pizSelect而是name="pizSelect"在 ids 中輸入錯誤,沒有使用 var 或 let 初始化totalInvoice以及 forloop 中缺少條件 i<pzArray.length。


至于單選按鈕,你可以這樣做document.querySelector("input[name=pzSelect]:checked").value 來獲取值


var pzArray = [];

function pizzaOrder() {

 document.getElementById("showlist").innerHTML

  var orderList = {

    pizzaName:"",

    size:"",

    topping:""

  };

  orderList.pizzaName = document.querySelector("input[name=pzSelect]:checked").value;

  orderList.size = document.getElementById("psize").value;

  let toppings=document.querySelectorAll("input[name=topping]:checked");

  toppings.forEach( (topping)=> orderList.topping+=topping.value+"," ) ;

  let totalInvoice="";

  pzArray.push(orderList);

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

    let pizza = pzArray[i];

    invoice = `Order number : ${i+1} ${pizza.size} ${pizza.pizzaName} with ${pizza.topping}`;

    totalInvoice += invoice + "<br>"

  }

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

}

<h4>Order Pizza</h4>

<div>

    <!-- in your code you have not included pzSelect in " " -->

    <input type=radio id="pzName" name="pzSelect" value="Chicago">Chicago Pizza

    <input type=radio id="pzName" name="pzSelect" value="Sicilian">Sicilian Pizza

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

Pizza    <br><br>

    Size

    <select id="psize">

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

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

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

    </select>


    <br><br>


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

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

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


    <br><br>


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


</div>


<span id="showlist"><span>


查看完整回答
反對 回復 2023-10-17
?
森欄

TA貢獻1810條經驗 獲得超5個贊

正確初始化您的 orderList 對象。首先作為一個空對象:


var orderList = {};

// then adding the attributes

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

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

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

或者直接初始化對象屬性


var orderList = {

    pizzaName = document.getElementById("pzName").value,

    size = document.getElementByName("pzSize").value,

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

}


查看完整回答
反對 回復 2023-10-17
  • 2 回答
  • 0 關注
  • 131 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號