2013-04-03 40 views
0

我有一個表,並有一個未定義的行數,這意味着人們可以添加/刪除行客戶端。我想要做的是當有人在兩個輸入(數量和比率)中輸入新值時,我想做一些加法和乘法並將其作爲總和輸出到頁面。如何使用jQuery來總結表中未定義數量的輸入?

每一行都有數量和比率。每行應該有一個總數=數量*比率。那麼,我想總結總計的所有總數。我希望能夠在用戶輸入他們的信息時做到這一點。這裏的領域是什麼樣子:

 <tbody id='line-items'> 
     <tr class='fields'> 
      <td><input id="invoice_invoice_line_items_attributes_0__destroy" name="invoice[invoice_line_items_attributes][0][_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields" data-association="invoice_line_items">X</a></td> 
      <td><input id="invoice_invoice_line_items_attributes_0_description" label="false" name="invoice[invoice_line_items_attributes][0][description]" size="30" type="text" /></td> 
      <td><input class="input-mini" id="invoice_invoice_line_items_attributes_0_quantity" label="false" name="invoice[invoice_line_items_attributes][0][quantity]" size="30" type="text" /></td> 
      <td><input class="input-mini" id="invoice_invoice_line_items_attributes_0_rate" label="false" name="invoice[invoice_line_items_attributes][0][rate]" size="30" type="text" /></td> 
     </tr> 
     <tr class='fields'> 
      <td><input id="invoice_invoice_line_items_attributes_1__destroy" name="invoice[invoice_line_items_attributes][1][_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields" data-association="invoice_line_items">X</a></td> 
      <td><input id="invoice_invoice_line_items_attributes_1_description" label="false" name="invoice[invoice_line_items_attributes][1][description]" size="30" type="text" /></td> 
      <td><input class="input-mini" id="invoice_invoice_line_items_attributes_1_quantity" label="false" name="invoice[invoice_line_items_attributes][1][quantity]" size="30" type="text" /></td> 
      <td><input class="input-mini" id="invoice_invoice_line_items_attributes_1_rate" label="false" name="invoice[invoice_line_items_attributes][1][rate]" size="30" type="text" /></td> 
     </tr> 
     <tr class='fields'> 
      <td><input id="invoice_invoice_line_items_attributes_2__destroy" name="invoice[invoice_line_items_attributes][2][_destroy]" type="hidden" value="false" /><a href="javascript:void(0)" class="remove_nested_fields" data-association="invoice_line_items">X</a></td> 
      <td><input id="invoice_invoice_line_items_attributes_2_description" label="false" name="invoice[invoice_line_items_attributes][2][description]" size="30" type="text" /></td> 
      <td><input class="input-mini" id="invoice_invoice_line_items_attributes_2_quantity" label="false" name="invoice[invoice_line_items_attributes][2][quantity]" size="30" type="text" /></td> 
      <td><input class="input-mini" id="invoice_invoice_line_items_attributes_2_rate" label="false" name="invoice[invoice_line_items_attributes][2][rate]" size="30" type="text" /></td> 
     </tr> 
     </tbody> 

我如何去這樣做上使用jQuery行的變量數,然後輸出該頁面?謝謝!

編輯:我就死在試圖找出如何從每一行獲得正確的輸入,像這樣很早就:

$('table input[id^=invoice_invoice_line_items_attributes]') 

其抓住所有的行,但我不知道如何跳過在ID中輸入數字並檢查ID的最後一個單詞以確保它是數量或比率。

+0

告訴我們你已經嘗試了什麼,我們不給你寫代碼 – Grumpy 2013-04-03 20:44:45

回答

0
var sum = 0; 
$('#line_items input[id$="quantity"]').each(function() { 
    sum += parseInt($(this).val()); 
}); 

您還可以使用parseFloat如果數字是漂浮