简体   繁体   English

使用javascript计算在线订单总计

[英]Calculating the total of an online order form using javascript

I'm working on a basic order form and I would like to see if I can build it from stratch. 我正在处理一个基本的订单表格,我想看看我是否可以从stratch构建它。

I have a form which is made up of a table with 12 items listed and text input fields for item quantity. 我有一个表格,其中包含一个列出了12个项目的表格,以及项目数量的文本输入字段。 What I would like to do is have the code run through the form to check for any quantities that have been entered, then relate these values to the prices in an array. 我想要做的是让代码在表单中运行以检查已输入的任何数量,然后将这些值与数组中的价格相关联。

So far I have the array: 到目前为止我有阵列:

var juicePrice = new Array();
juicePrice["citrusZing"]=20.00;
juicePrice["strawberrySmooth"]=22.00;
juicePrice["carrotSpin"]=21.50;
juicePrice["Tropical"]=20.75;
...

and a way to run through the form: 以及贯穿表单的方法:

calculateTotal()    
{   
var juiceForm = document.forms["juiceform"];

    //Get a reference to the juice the user Chooses name=selectedJuiceQty":
    var selectedJuiceQty = juiceForm.elements["selectedJuiceQty"];

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

but I'm not quite sure how to connect the information from the form to calculate the totals. 但我不太清楚如何连接表单中的信息来计算总数。 Can anyone point me in the right direction? 谁能指出我正确的方向? Is it something like this? 这是这样的吗?

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

    if(selectedJuiceQty[i]>0) {
        juiceTotal += juicePrice[selectedJuiceQty[i].value]*selectedJuiceQty;
        //If we get a match then we break out of this loop
        break;
    }
    return total;
}

Is it possible to use the same name tag for each field or should I just use citrusZingQty = parseInt(document.getElementById("citrusZing").value); 是否可以为每个字段使用相同的名称标签,或者我应该使用citrusZingQty = parseInt(document.getElementById("citrusZing").value); for each item? 对于每个项目? Then I would have to list all of the items, which doesn't seem a very elegant way. 然后我必须列出所有项目,这似乎不是一个非常优雅的方式。 What would happen if multiple items are selected? 如果选择多个项目会发生什么?

Any help anyone can give to point me in the right direction would be great. 任何人都可以帮助指出我正确的方向将是伟大的。

So you can do what you want. 所以你可以做你想做的事。 Michael pointed this out in the comments but it may have been overlooked. 迈克尔在评论中指出了这一点,但它可能被忽略了。

var myPrices = new Object();
myPrices['eggs'] = 1.50;
myPrices['bread'] = 1.00;
// ...

Then you can loop through your form fields and check against your 'myPrices' object. 然后,您可以遍历表单字段并检查“myPrices”对象。

EDIT 编辑

To answer your question in the comments - I would not use the same name for all of my input fields. 要在评论中回答您的问题 - 我不会对所有输入字段使用相同的名称。 It does make looping through the form easier perhaps. 它确实可以使表单更容易循环。 However, using the id/class of the input tag is not a nice solution in my opinion. 但是,在我看来,使用输入标记的id / class并不是一个很好的解决方案。 HTML id/class are there for CSS/Javascript manipulation and using it to identify which fruit the input represents will not be apparent to other developers working on the project (I realize this may be a small project for you but it's best not to start any bad habits). HTML id / class用于CSS / Javascript操作,并使用它来识别输入代表哪些水果对于项目的其他开发人员来说不明显(我意识到这可能是一个小项目,但最好不要启动任何陋习)。 I would name each input after the juice/drink it represents. 我会在它代表的果汁/饮料之后命名每个输入。

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

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