Javascript 如何使用jQuery汇总表中未定义数量的输入?
我有一个表,其中有未定义的行数,这意味着用户可以在客户端添加/删除行。我想做的是,当有人在两个输入(数量和速率)中输入新值时,我想做一些加法和乘法,并将其作为和输出到页面 每行有数量和比率。每行应有一个总计=数量*比率。然后,我想把所有的总数加起来算作总数。我希望能够做到这一点,作为一个用户客户端输入他们的信息。以下是字段的外观:Javascript 如何使用jQuery汇总表中未定义数量的输入?,javascript,jquery,Javascript,Jquery,我有一个表,其中有未定义的行数,这意味着用户可以在客户端添加/删除行。我想做的是,当有人在两个输入(数量和速率)中输入新值时,我想做一些加法和乘法,并将其作为和输出到页面 每行有数量和比率。每行应有一个总计=数量*比率。然后,我想把所有的总数加起来算作总数。我希望能够做到这一点,作为一个用户客户端输入他们的信息。以下是字段的外观: <tbody id='line-items'> <tr class='fields'> <
<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>
它抓取了所有的行,但我不确定如何跳过ID中的数字,并检查ID的最后一个字,以确保它是数量或速率
var sum = 0;
$('#line_items input[id$="quantity"]').each(function() {
sum += parseInt($(this).val());
});
如果数字是浮点数,也可以使用parseFloat显示您的尝试,我们不为您编写代码
var sum = 0;
$('#line_items input[id$="quantity"]').each(function() {
sum += parseInt($(this).val());
});