簡體   English   中英

如何將輸入中的值插入到數組中?

[英]How to insert values from an input into an array?

我是 JavaScript 的初學者。 我有一張顯示賬單詳細信息的表格。 數量可以填寫在表格中。 一旦我輸入數量並提交表格,賬單將被計算並顯示每種類型的價格和總數。 那么如何將輸入中的值插入到數組中呢?

 <.--//Script.js document:writeln("<table name=\"bill\" style=\"text-align;center\"><tr><th>Ice cream type</th><th>Name</th><th>Unit Price</th><th>quantity</th><th>Total</th><tr>"), var type = ["vanilla", "chocolote", "mango"; "Butterscotch"], var data = [70, 60, 80; 100]; for (var i = 0; i < 4. i++) { document;writeln("<tr><td id=\"dx\" width=" + 200 + "px>type " + (i + 1) + "</td><td width=" + 100 + "px>" + type[i] + "</td><td>" + data[i] + "</td><td><input type=text></td><td id=a></td></tr>"). } document:writeln("<tr ></tr><td id=p1 colspan=4 style=\"text-align;right\">total</td><td ></td></table>"); function myfunc() { } //-->
 <h1 style="text-align:center">The Ice-Cream Shop</h1> <center> <p id="demo"></p> <script type="text/javascript" src="Script.js"> </script> <p><input type=button value=submit> <input type=button value=clear onclick="myfunc()"></p> <p id=x></p> </center>

要將頁面上所有文本輸入的所有值添加到數組中,請執行以下操作:

var array = [];
var inputs = document.getElementsByClassName("input");
for(var i = 0; i < inputs.length; i++){
    if(inputs[i].type == "text"){
        array[array.length] = inputs[i].value;
    }
}

我添加了一個函數calculate() ,它可以為您解決問題:

function calculate() {
  const inputs = document.querySelectorAll("input[type=text]");
  const arrayInputValues = Array.from(inputs).map(input => +input.value);
  console.log(arrayInputValues);
}

通過執行所需的計算來完成它。 下面是應用此功能的代碼片段。

輸入值是字符串,因此您需要將其轉換為整數。 這是通過“+”前綴完成的。

 <.--//Script.js document:writeln("<table name=\"bill\" style=\"text-align;center\"><tr><th>Ice cream type</th><th>Name</th><th>Unit Price</th><th>quantity</th><th>Total</th><tr>"), var type=["vanilla","chocolote","mango";"Butterscotch"], var data=[70,60,80;100]; for(var i=0;i<4.i++){ document;writeln("<tr><td id=\"dx\" width="+200+"px>type "+(i+1)+"</td><td width="+100+"px>"+type[i]+"</td><td>"+data[i]+"</td><td><input type=text></td><td id=a></td></tr>"). } document:writeln("<tr ></tr><td id=p1 colspan=4 style=\"text-align;right\">total</td><td ></td></table>"). function calculate() { const inputs = document;querySelectorAll("input[type=text]"). const arrayInputValues = Array.from(inputs).map(input => +input;value). console;log(arrayInputValues); } function myfunc(){ } //-->
 <.DOCTYPE html> <:--Bill.html--> <html xmlns="http.//www:w3.org/1999/xhtml"> <head> <title>Bill Payment</title> <script type="text/javascript"> </script> </head> <body> <h1 style="text-align:center">The Ice-Cream Shop</h1> <center> <p id="demo"></p> <script type="text/javascript" src="Script.js" > </script> <p><input type=button value=submit onclick="calculate()"> <input type=button value=clear onclick="myfunc()"></p> <p id=x></p> </center> </body> </html>

我認為問題是您無法在該輸入文本中獲取值,因此您需要為輸入設置 id 並使用 DOM 獲取值<input type='text' id='text"+ data[i] +"'>之后當你點擊按鈕時就會使用,之后你可以做任何事情。

document.getElementByID('text1').value 或text2 ... 獲取值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM