2015-03-13 61 views
0

我已經看過jQuery和添加列值的幾個例子;然而,這些例子只有一列和一個值。將特定列相加或相乘

我想知道你是否有多列,那麼你如何抓特定的?

例如,如果我想添加所有的數量列在一起乘法費用列與其相鄰數量

到目前爲止,我已經開始設置,但它不起作用。最初我試過

child_nodes.eq(2).text() 

獲得數量的列,但我知道我的邏輯是關閉的。

$(document).ready(function() { 

$('#checkout').on('click', function() { 

     item_count(); 
}); 
}); 

function item_count(){ 

    $('.product').each(function() { 

     //find number of child nodes 
     var child_nodes = $(this).text(); //children returns all 

     var count = ""; 
     for (var i=0; i<child_nodes.length; i++){ 
     //whole row output 
     count += child_nodes.eq(i).text() + "\n"; 
     } 
    }); 

    //var total = parseInt($('.product').val()); 
    var total += parseInt(count); 
    //update value 
    return $('#nitems').text(total); 

}; 

我剛開始嘗試j查詢,所以我覺得我的代碼看起來很不喜歡,所以我模擬。

<h4>Shopping Cart</h4> 
<table class='table'> 
    <thead> 
    <tr> 
     <th>Product</th><th>Unit cost</th><th>Quantity</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr class='product'> 
     <td>Milk Chocolate</td> 
     <td>7.48</td> 
     <td>5</td> 
    </tr> 
    <tr class='product'> 
     <td>Assorted Fine Chocolates</td> 
     <td>9.98</td> 
     <td>7</td> 
    </tr> 

    <tr class='product'> 
     <td>Assorted Milk & Dark Chocolates</td> 
     <td>12.98</td> 
     <td>9<td> 
    </tr> 

    <tr class='product'> 
     <td>Assorted Dessert Truffles</td> 
     <td>15.98</td> 
     <td>4</td> 
    </tr> 
    </tbody> 
    </table> 
</div> 
</div> 
<div class='row'> 
<div class='col-md-4'> 

    <h3>Finalize Sale </h3> 
    <table class='table'> 
    <tr> 
     <td>Total Items</td> 
     <td><span id="nitems" >0</td> 
    </tr> 
    <tr> 
     <td>Subtotal</td> 
     <td><span id="subtotal" >0</td> 
    </tr> 
    <tr> 
     <td>5% Sales tax</td> 
     <td><span id="tax" >0</td> 
    </tr> 
    <tr> 
     <td>Total</td> 
     <td><span id="total" >0</td> 
    </tr> 
    <tr> 
     <td>Final amount (with 15% discount)</td> 
     <td><span id="final" >0</td> 
    </tr> 
    </table> 
+0

您能否提供比「不起作用」更多的細節?它是否給出錯誤?沒有給你想要的結果? – Scottie 2015-03-13 14:06:04

+0

唯一的錯誤是我顯然缺少一個; ...從 'count + = child_nodes.eq(i).text()+「\ n」; } }); // var total = parseInt($('。product')。val()); var total + = parseInt(count);' – narue1992 2015-03-13 14:12:19

+0

@Scottie除此之外,什麼都不打印 – narue1992 2015-03-13 14:12:57

回答

0

.each函數應該接受一個將用作迭代器的變量。更改此:那裏面

$('.product').each(function() { 

這個

$('.product').each(function(index, val) { 

,你應該有訪問每個TD的的。

另外,該線路:

count += child_nodes.eq(i).text() 

將會給你的5794的值它附加每個串的,不加整型值。你會想是這樣的:

count += parseInt($("td", val).eq(3)), 10); 

爲了把它放在一起,我會做到這一點...

function item_count(){ 

    var count = 0; 

    $('.product').each(function(index, val) { 

     var total += parseInt($("td", val).eq(3), 10); 
    }  

    $('#nitems').val(total); 
}; 

注意,這是所有未測試的,所以有些調整可能是必要的。

+0

所以我是jquery的新手,所以你可以解釋value索引和val是如何自動理解值的嗎?換句話說,我沒有看到你宣佈他們。另外「10」是什麼意思? – narue1992 2015-03-13 14:20:50

+0

老實說,我不清楚.each的內部工作如何分配索引和val字段。這對我來說是一件很好的事情!只需說,它就可以工作。出於某種神祕原因,默認情況下,parseInt使用基數8。以下是描述它的鏈接:http://stackoverflow.com/questions/5600366/why-does-the-radix-for-javascripts-parseint-default-to-8 – Scottie 2015-03-13 16:14:47