1

Qty、Item name、Price、および Total Price を含むフォームがあります。アイテム名と価格は、XML ファイルから取得する情報です。数量フィールドは、ユーザーが必要な数量を入力できる場所です。

私がする必要があるのは、ユーザーが特定のアイテムの数量を入力すると、その数量を取り、その特定のアイテムの価格を掛けて、合計価格フィールドに表示することです。次に、その合計価格フィールドが合計されて小計になります。価格は、XML ファイルから取得したセット数です。数量はユーザーによって異なります。すべてのアイテムを購入する必要はありません。

例: アイテム 1 の数量: 3 : $10 -> $30 アイテム 2 の数量: 2: $5 --> $10 小計: $40 これはすべて、ユーザーが数量を入力したときに発生します。これまでのところ、XML からデータを 1 つの配列に取得しています。そして、ユーザーの数量を別の配列に入れます。しかし、それを機能させる方法がわかりません。ありがとうございました。

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

2 に答える 2

0

これを試して:

var total = 0;

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

alert(total);
于 2012-12-09T06:15:43.860 に答える
0

QtyValue2 つの配列がand関数によって返されると仮定すると、2Priceつの配列を同時にループして小計を取得できます。(同じ長さであることが保証されていますよね?)

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
于 2012-12-09T06:11:46.800 に答える