2012-10-23 68 views
1

所以我基本上具有在格式多行:遍歷表格行計算總計(數量*每行成本)

<tr class="items" id="items"> 
    <td><input type="text" name="description[]" id="description[]" style="width:450px;" /></td> 
    <td><input type="text" name="quantity[]" id="quantity[]" style="width:40px;text-align:center;" onblur="CaclulateQuantityTotal();"/></td> 
    <td><input type="text" name="cost[]" id="cost[]" style="width:40px;text-align:center;" onblur="CaclulateCostTotal();"/></td> 
    <td><select name="currency[]" id="currency[]"><option value="USD">USD</option><option value="CAD">CAD</option></select></td> 
    <td style="text-align:right;"><input type="text" name="total[]" id="total[]" style="width:40px;text-align:center;" readonly="readonly" /></td> 
    <td><a href="#" id="add_item">+</a></td> 
</tr> 

我基本上想總[]是(該行)成本*數量。

我的問題是我知道如何循環遍歷所有數量[]和貨幣[]使用jquery,但我不知道如何匹配它們在每行的基礎上。

任何人都可以幫忙嗎?

+4

首先,從您的所有元素中刪除的ID - 你不應該有一個頁面上有兩個元素具有相同的ID 。 – Blazemonger

+0

在「tr」上使用.each獲得行 – Flo

+0

'$('tr.items')。each(function(index,cItem){var jItem = $(cItem); jItem.find('input [name^('input^[name] = value]')。val(parseFloat(jItem.find('input [name^= cost]').val())* parseFloat(jItem.find ));});' – Orbling

回答

6
$('tr.items').each(function(i,el) { 
    var $this = $(this), 
     $cost = $this.find('[name="cost\\[\\]"]'), 
     $quant = $this.find('[name="quantity\\[\\]"]'), 
     c = parseFloat($cost.val()), 
     q = parseInt($quant.val(), 10), // always use a radix 
     total = c * q || 0; // default value in case of "NaN" 
    $this.find('[name="total\\[\\]"]').val(total.toFixed(2)); 
}); 

http://jsfiddle.net/mblase75/AtcPc/

+1

你需要''.value()''.val()',因爲它們是字符串。 – Orbling

+0

他們會自動轉換爲JavaScript。然而,你是對的 - 它們可能應該被預先解析爲整數或浮點數。更新。 – Blazemonger

+0

JS通常不會在這個方向上自動轉換,它會編號 - >字符串,但反之亦然。 – Orbling

1

你可以遍歷$( 「項目 」),並比較$(項目[1])。兒童(「 成本」)和$(項目[1])。兒童(「量」)(當然,你必須補充一點,作爲一個階級對那些投入)

因此,像 -

var items = $(".items").get(); 

for(var i = 0; i < items.length; i++) 
{ 
    var cost = $(items[i]).children(".cost").val() * $(items[i]).children(".quantity").val(); 
} 

這可能不準確,但它應該讓你開始。

+0

+1;儘管'.children()'只能匹配直接的孩子,'​​'會阻礙,使用'.find()'來獲得後代匹配。 – Orbling

0

在函數調用中添加對象從HTML

HTML

<tr class="items" id="items"> 
     <td><input type="text" name="description[]" id="description[]" /></td> 
     <td><input type="text" name="quantity[]" id="quantity[]" onblur="CaclulateQuantityTotal(this);"/></td> 
     <td><input type="text" name="cost[]" id="cost[]" onblur="CaclulateCostTotal(this);"/></td> 
     <td><select name="currency[]" id="currency[]"><option value="USD">USD</option><option value="CAD">CAD</option></select></td> 
     <td><input type="text" name="total[]" id="total[]" readonly="readonly" /></td> 
     <td><a href="#" id="add_item">+</a></td> 
</tr> 

的Javascript

function CaclulateQuantityTotal(obj) { 
    var $tr = $(obj).closest('tr.items'); 
    //you now have access to the tr the function is called from, do whatever you want! 
    var $q = $tr.find('input[name=quantity]'); // gives you quantity, etc... 
} 
0

添加一些類,所以你可能會得到的值更容易,再加上...你不應該有重複的ID :)

 <tr class="items" id="items"> 
      <td><input type="text" name="description[]" id="description[]" style="width:450px;" /></td> 
      <td><input type="text" name="quantity[]" id="quantity[]" style="width:40px;text-align:center;" onblur="CaclulateQuantityTotal();" class="quantity"/></td> 
      <td><input type="text" name="cost[]" id="cost[]" style="width:40px;text-align:center;" onblur="CaclulateCostTotal();" class="cost"/></td> 
      <td><select name="currency[]" id="currency[]"><option value="USD">USD</option><option value="CAD">CAD</option></select></td> 
      <td style="text-align:right;"><input type="text" name="total[]" id="total[]" style="width:40px;text-align:center;" readonly="readonly" class="total"/></td> 
      <td><a href="#" id="add_item">+</a></td> 
     </tr> 

jQuery代碼:

(function() { 
    $.each($('tr.items'), function(){ 
     var quantity = $(this).children('td').children('.quantity').val(); 
     var cost = $(this).children('td').children('.cost').val(); 
     var total = quantity*cost; 
     $(this).children('td').children('.total').val(total); 
    }); 
}()); 

希望它能幫助,這裏的工作演示:http://jsfiddle.net/MvFYd/

+0

此外,你只需要運行這個onLoad,所有的價格總計將立即計算! ;) –