简体   繁体   English

数量变化时如何计算表格中一行的总和

[英]How to calculate total of a row in a table when quantity changes

I am trying to update the total of each row with increasing quantity but only one row is updated.我正在尝试通过增加数量来更新每行的总数,但只更新了一行。 How can I update each row?如何更新每一行? How can I add id in these to make more dynamic?如何在这些中添加 id 以使其更具活力? I am using jQuery version 2.2.4.我正在使用 jQuery 版本 2.2.4。 Thanks in advance.提前致谢。

<tr>
  <td class="col-sm-8 col-md-6">
    <div class="media">
      <a class="thumbnail pull-left" href="#"> <img src="img/bg-img/b3.jpg" style="width: 72px; height: 72px;" alt=""> </a>
      <div class="media-body">
        <h5 class="media-heading">
          <?php echo isset($carts['title']) ? $carts['title']: ''; ?>
        </h5>
      </div>
    </div>
  </td>
  <td class="col-sm-1 col-md-1 quantity" style="text-align: center">
    <input type="number" class="form-control" id="fname" oninput="myFunction()" value="1" label="">
  </td>
  <td class="col-sm-1 col-md-1 text-center pr-01 price"><strong><?php echo isset($carts['sale_price']) ? $carts['sale_price']: $carts['regular_price'];  ?></strong></td>
  <input type="hidden" class="form-control" id="artprice" value="<?php echo isset($carts['sale_price']) ? $carts['sale_price']: $carts['regular_price'];  ?>" label="">
  <td class="col-sm-1 col-md-1 text-center pr-01" id="total"><strong>$14.61</strong></td>
  <td class="col-sm-1 col-md-1">
    <div class="product" data-id="<?php echo $key; ?>">
      <button type="button" class="btn btn-danger remove-from-cart">Remove</button>
    </td>
  </div>
</tr>
function myFunction() {
  var x = document.getElementById("fname") value;
  var str = $("#artprice").val();
  $('#total').html(str * x);
}

There's several issues in your code.您的代码中有几个问题。

  • There's a </div> in the wrong place.有一个</div>在错误的地方。 It's outside the <td> it belongs to它在它所属的<td>之外
  • The #artprice input is outside the table structure. #artprice输入在表结构之外。 It needs to be placed within a td它需要放在td
  • There's a missing .有一个失踪. before the value property in the JS.在 JS 中的value属性之前。

However the biggest issue is because your repeat the same id attributes on multiple elements as you loop through to create these rows in the HTML.然而,最大的问题是,当您在 HTML 中循环创建这些行时,您在多个元素上重复相同的id属性。 id have to be unique within the DOM. id在 DOM 中必须是唯一的。

To fix this use common class attributes on all the elements you need to find instead, then use jQuery's DOM traversal techniques to retrieve them based on the current row.要解决此问题,请在您需要查找的所有元素上使用常见的class属性,然后使用 jQuery 的 DOM 遍历技术根据当前行检索它们。

Also note in the below example that I used an unobtrusive event handler.另请注意,在下面的示例中,我使用了一个不显眼的事件处理程序。 Inline event attributes are no longer good practice and should be avoided where possible.内联事件属性不再是好的做法,应尽可能避免。 In addition I removed the inline style attributes and used rules in an external stylesheet.此外,我删除了内联style属性并在外部样式表中使用了规则。 Both of these approaches are preferred due to the Separation of Concerns principle.由于关注点分离原则,这两种方法都是首选。

With all that said, try this:说了这么多,试试这个:

 jQuery($ => { $('.fname').on('input', function() { let $row = $(this).closest('tr'); let price = $row.find(".artprice").val(); $row.find('.total strong').text('$' + (this.value * price).toFixed(2)); }); });
 .media a img { width: 72px; height: 72px; } td.quantity { text-align: center; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <table> <tr> <td class="col-sm-8 col-md-6"> <div class="media"> <a class="thumbnail pull-left" href="#"><img src="img/bg-img/b3.jpg" alt=""></a> <div class="media-body"> <h5 class="media-heading">Title</h5> </div> </div> </td> <td class="col-sm-1 col-md-1 quantity"> <input type="number" class="form-control fname" value="1" label=""> </td> <td class="col-sm-1 col-md-1 text-center pr-01 price"> <strong>$14.61</strong> <input type="hidden" class="form-control artprice" value="14.61" label=""> </td> <td class="col-sm-1 col-md-1 text-center pr-01 total"><strong>$14.61</strong></td> <td class="col-sm-1 col-md-1"> <div class="product" data-id="Key"> <button type="button" class="btn btn-danger remove-from-cart">Remove</button> </div> </td> </tr> <tr> <td class="col-sm-8 col-md-6"> <div class="media"> <a class="thumbnail pull-left" href="#"><img src="img/bg-img/b3.jpg" alt=""></a> <div class="media-body"> <h5 class="media-heading">Title</h5> </div> </div> </td> <td class="col-sm-1 col-md-1 quantity"> <input type="number" class="form-control fname" value="1" label=""> </td> <td class="col-sm-1 col-md-1 text-center pr-01 price"> <strong>$57.24</strong> <input type="hidden" class="form-control artprice" value="57.24" label=""> </td> <td class="col-sm-1 col-md-1 text-center pr-01 total"><strong>$57.24</strong></td> <td class="col-sm-1 col-md-1"> <div class="product" data-id="Key"> <button type="button" class="btn btn-danger remove-from-cart">Remove</button> </div> </td> </tr> </table>

One last thing to note is that jQuery 2.2.4 is getting a little old now.最后要注意的一件事是 jQuery 2.2.4 现在有点老了。 You should consider upgrading to 3.x您应该考虑升级到 3.x

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

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