简体   繁体   English

输入计算

[英]Inputs calculation

I have several tables which looks like these below: 我有几个表如下所示:

<table id="table1">
<tr>
    <th>Item</th>
    <th>Price</th>
</tr>
<tr>
    <td>Item 1</a></td>
    <td><input type="text" name="sum1" id="sum1" /></td>
</tr>
<tr>
    <td>Item 2</a></td>
    <td><input type="text" name="sum2" id="sum2" /></td>
</tr>
<tr>
    <td>Item 3</td>
    <td><input type="text" name="sum3" id="sum3" /></td>
</tr>
<tr>
    <td>&nbsp;</td>
    <td>Total:</td>
    <td>$total </td>
</tr>
</table>



<table id="table2">
<tr>
    <th>Item</th>
    <th>Price</th>
</tr>
<tr>
    <td>Item 1</a></td>
    <td><input type="text" name="sum1" id="sum1" /></td>
</tr>
<tr>
    <td>Item 2</a></td>
    <td><input type="text" name="sum2" id="sum2" /></td>
</tr>
<tr>
    <td>Item 3</td>
    <td><input type="text" name="sum3" id="sum3" /></td>
</tr>
<tr>
    <td>Item 4</td>
    <td><input type="text" name="sum4" id="sum4" /></td>
</tr>
<tr>
    <td>&nbsp;</td>
    <td>Total:</td>
    <td>$total</td>
</tr>
</table>

Does anybody know if there is a possibility to sum item prices for each input (seperately for table1, table2, etc..) And at the end print sum of items from all items? 有没有人知道是否有可能为每个输入项目的价格加起来(单独为table1,table2等...)并在最后打印所有项目的项目总和?

You'll need a dom manipulator like jQuery or simply javascript. 你需要像jQuery这样的dom操纵器或简单的javascript。 I'd give the fields a class to make it even easier--say, "sum". 我会给田地上一堂课,让它更容易 - 比方说,“总和”。 Then you can grab all the children "sum"s from the parent tables. 然后你可以从父表中获取所有子项“sum”。 Parse their value to an int and add. 将它们的值解析为int并添加。 Not too bad. 还不错。

Here's a working JS fiddle example 这是一个有效的JS小提琴示例

this should be pretty easy with jquery, assuming you can add a class name called "line-item" or something to each input and a class name "total" to the cell which should display calculated sum, you can do 使用jquery这应该很容易,假设你可以为每个输入添加一个名为“line-item”的类名,并且对于应该显示计算总和的单元格的类名“total”,你可以做

function calculateSum(tableId){
 var sum = 0;
 $(tableId + ' .line-item').each(function(){
   var value = parseFloat($(this).val().trim());
   if(value){
    sum += parseFloat($(this).val());
   }
 });
 $(tableId + ' .total').html(sum);
}

and invoke this function with the id of the table for which you want. 并使用您想要的表的id调用此函数。

If you cannot add a class name, this don't get tuffer even a single bit, but instead of .line-item try to put just input , it should do the trick in this scenario. 如果你不能添加一个类名,那么即使只有一个位也不会得到tuffer,但是在.line-item尝试只input ,它应该在这个场景中完成。 Instead of .total you can write tr:last-child td:nth-child(2) 而不是.total你可以写tr:last-child td:nth-child(2)

UPDATE: added "parseInt" to avoid string concatenation 更新:添加“parseInt”以避免字符串连接

Better way is not to call different elements with similar IDs, it's invalid way. 更好的方法是不要调用具有相似ID的不同元素,这是无效的方式。 But even if so - you can perform calculations for every table simply working with inputs in this table. 但即便如此 - 您只需使用此表中的输入即可对每个表执行计算。

Algorithm: 算法:

  1. You run loop for all <table> tags. 您为所有<table>标记运行循环。
  2. For every <table> tag you find all <input>s (no matter what IDs they have); 对于每个<table>标签,您可以找到所有<input> s(无论它们具有哪些ID);
  3. Calculate total sum for all inputs for current table. 计算当前表的所有输入的总和。

You can do this with JavaScript but better for you is jQuery. 你可以用JavaScript做到这一点,但jQuery更适合你。

BUT: don't call HTML DOM elements with similar IDs. 但是:不要使用类似的ID调用HTML DOM元素。 It's wrong!!! 这是不对的!!!

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

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