繁体   English   中英

如何使用jQuery汇总表中未定义数量的输入?

[英]How do I use jQuery to sum up an undefined number of inputs in a table?

我有一个表,并且行数不确定,这意味着人们可以在客户端添加/删除行。 我想做的是,当某人在两个输入(数量和比率)中输入新值时,我想做一些加法和乘法运算并将其作为总和输出到页面。

每行都有数量和比率。 每行应有总计=数量*费率。 然后,我想对总计进行总计。 我希望能够在用户输入其信息时在客户端进行此操作。 这些字段如下所示:

     <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的最后一个字以确保它是数量或比率。

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

如果数字为浮点数,也可以使用parseFloat

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM