Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

javascript - 如何将输入中的值插入数组?(How to insert values from an input into an array?)

I am a beginner in JavaScript.

(我是JavaScript的初学者。)

I have a table which shows details of a bill.

(我有一张桌子,上面有帐单的细节。)

Quantity can be filled in the form.

(数量可以填写在表格中。)

Once I enter the quantities and submit the form, the bill will be calculated and display the price of each type, and the total.

(输入数量并提交表格后,将计算账单并显示每种类型的价格以及总计。)

So how to insert values from input into an array?

(那么如何将输入中的值插入数组呢?)

 <!--//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> 

  ask by Aye translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To add all the values of all the text inputs on a page into an array, you do the following:

(要将页面上所有文本输入的所有值添加到数组中,请执行以下操作:)

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...