【问题标题】:multiply index of one array to a corresponding index of another array. The add all the result together. Javascript将一个数组的索引乘以另一个数组的对应索引。将所有结果加在一起。 Javascript
【发布时间】:2012-11-26 22:11:27
【问题描述】:

我有一个包含数量、项目名称、价格和总价的表格。商品名称和价格是我将从 XML 文件中检索的信息。数量字段是用户可以输入他们想要的任何数量的地方。

我需要做的是当用户输入某个商品的数量时,它将采用该数量并乘以该特定商品的价格,然后将其显示在“总价”字段中。然后,该总价格字段将被加在一起以给出小计。价格是从 XML 文件中检索的一组数字。数量根据用户而变化。他们不必购买所有物品。

例如:数量:第 1 件的 3 件:10 美元 -> 30 美元 数量:2 项目 2:5 美元 --> 10 美元 小计:40 美元 当用户输入他们的数量时,所有这一切都会发生。到目前为止,我将 XML 中的数据检索到一个数组中。并将用户 qty 放入另一个数组。但我不知道如何使它工作。谢谢。

HTML 代码:

    <div id="order"> 
        <table class="bottomBorder" id="torder">
        <tr class="style3"><th align="center">Quantity</th>
            <th width="200px">Item Description</th>
            <th>Price</th>
            <th>Total Price</th> 

        <script>
    var xmlDoc = getXML();
    var x=xmlDoc.getElementsByTagName("item");

    for (i=0;i<x.length;i++)
    {
        var Qty = "Qty" + i;
        var totalprice = "totalprice" + i;

        document.write('<tr><td align="center">');
        document.write('<input type= "text" id="' + Qty +'" name="txtQty" size="2" maxlength="4"/>');
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write(x[i].getElementsByTagName("price")[0].childNodes[0].nodeValue);
        document.write("</td><td>");
        document.write('<span id="'+totalprice+'"></span>');
         document.write("</td></tr>");
        }
        </script>                       
        </table>

        <table class="bottomBorder">
          <tr>
            <td width="323" class="style4" align="right" >Subtotal</td>
            <td width="75" align="right" id="subtotal"></td>
          </tr>
           </table>

Javascript 代码:

    function QtyValue()
    {   
var Qtys =document.getElementById('torder').getElementsByTagName('input');
var Qty = [];//create array
var error2 = document.getElementById("errorinfo2");

for (var i = 0, l = Qtys.length; i < l; ++i) { //looping through txtqty input 
       if (Qtys[i].value.length) {
        Qty.push(Qtys[i].value); //pushing value into Qty array
        }
}

if(Qty.every(isNumeric) /*&& Qty.length!==0*/) //check to see if info entered in array Qty is a number and is not empty
{
  return Qty;
}else{
     error2.style.display="block";
     error2.innerHTML = "*Please enter a valid number for your quantity";
     return false;
}
    }

    //Getting value for price from productlist.xml into an array
    function Price()
    {
var xmlDoc = getXML();

// Pull out the quote and author elements
var nPrice = xmlDoc.getElementsByTagName('price');

    // Create our two arrays
var arrPrice=[];

    // Loop through each quote elements yanking out the values and pushing them into the array
    for (i=0; i<nPrice.length; i++)
    {
         arrPrice.push(nPrice[i].childNodes[0].nodeValue);
    }
    return arrPrice;
        }

【问题讨论】:

    标签: javascript xml arrays


    【解决方案1】:

    试试这个:

    var total = 0;
    
    if (Qty.length == arrPrice.length) {
        for (var i = 0; i < Qty.length; ++i) {
            total += (Qty[i] * arrPrice[i]);
        }
    }
    
    alert(total);
    

    【讨论】:

    • 感谢您的回答。我使用代码的问题是,如果用户首先输入第 3 项的数量,它将乘以第 1 项的价格,而不是第 3 项的相应价格。知道如何解决这个问题吗?
    • 我认为你需要为数量创建一个二维数组。因此,如果用户先输入第 3 项的数量,您可以将其存储为 Qty[item No][item price],即Qty[2][0] = 1;。希望你明白我的意思。
    • 如果用户只是按任意顺序输入数量,这会起作用吗?
    【解决方案2】:

    假设您的两个数组由 QtyValuePrice 函数返回,您可以同时遍历这两个数组以获得小计。 (保证它们的长度相同,对吗?)

    var prices = Price();
    var quantities = QtyValue();
    var subtotal = 0;
    
    for (var i = 0; i < prices.length; i++) {
        var itemTotal = prices[i] * quantities[i];
    
        // todo: display 'itemTotal' on page
    
        subtotal += itemTotal;
    }
    
    // todo: display 'subtotal' on page
    

    【讨论】:

    • 是的,它们的长度相同。但是,用户可以输入他们选择的任何商品的数量,因此他们可能会跳过第 2 项,而只有第 1 项和第 3 项。我还需要显示与每个商品相对应的每件商品的价格 (qty*priceperitem)在添加小计之前。我似乎无法弄清楚。数据变化的事实让我感到困惑。非常感谢您的帮助。
    • 当您在QtyValue 中收集数量时,不要跳过空白字段,而是采用默认值(可能为 0 或 1)并推送到 Qty 数组。例如,在if (Qtys[i].value.length) {...} 之后放置else { Qty.push(0); }。这些帮助有用?我将用一条评论更新我的答案,说明如何显示每个项目的小计。
    • 嗯...理论上应该可以工作,但它似乎将 0 替换为我所有的值...。
    • 你还在努力吗?发布您更新的代码,我会尽力提供帮助。
    猜你喜欢
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 2023-01-13
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多