jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何通过使用jquery添加该表的行来获取表总数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要通过添加基于相同id的表行来获取表总数,但是这里第一个表它正确地出现但是对于第二个表,它添加一个表总计并显示总和.
如何避免将其添加到第二个表.
我的 HTML代码

<table>
  <tr style="BACkground:#2b2e76">
    <th colspan="1" style="padding: 0;">
      <p style="color:white">
        Table One

      </p>
    </th>
    <th>
      <p style="color:white" class="BundleB4936" id="B4936">@R_794_10586@l : <span class="BundelRow@R_794_10586@lB4936">1400.00</span></p>
    </th>
  </tr>
  <tr>
    <td>
      <input type="text" value="200.00" name="row_@R_794_10586@l[]" id="row@R_794_10586@l11_B4936" class="row@R_794_10586@l">
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" value="1200.00" name="row_@R_794_10586@l[]" id="row@R_794_10586@l12_B4936" class=" row@R_794_10586@l">
    </td>
  </tr>
</table>
<table>
  <tr style="BACkground:#2b2e76">
    <th colspan="1" style="padding: 0;">
      <p style="color:white">
        Table Two

      </p>
    </th>
    <th>
      <p style="color:white" class="BundleB1027" id="B1027">@R_794_10586@l : <span class="BundelRow@R_794_10586@lB1027">1750.00</span></p>
    </th>
  </tr>
  <tr>
    <td>
      <input type="text" value="100.00" name="row_@R_794_10586@l[]" id="row@R_794_10586@l16_B1027" class="row@R_794_10586@l">
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" value="250.00" name="row_@R_794_10586@l[]" id="row@R_794_10586@l17_B1027" class="row@R_794_10586@l">
    </td>
  </tr>
</table>

我的jquery:

var Bsum = 0;
    var BundelID = '';
    $(".row@R_794_10586@l").each(function() {
      var RowID = $(this).attr('id');
      var suffix = RowID.match(/\d+/)[0];
      BundelID = $('.bundleB' + suffiX).attr('id');

      if (RowID.indexOf(BundelID) != -1) {
        var BValue = $('#' + RowID).val();
        if (!isNaN(Bvalue)) {
          Bsum += parseFloat(Bvalue);
        }
      }
      $('.bundelRow@R_794_10586@l' + BundelID).html(parseFloat(Bsum).toFixed(2));

    });

这是我得到的输出

如何通过使用jquery添加该表的行来获取表总数

但我希望输出应该如下所示

如何通过使用jquery添加该表的行来获取表总数

有任何建议请!
谢谢.

解决方法

你刚遇到问题Ids Parse试试这个代码而不是你的代码

注 – >更改的行被评论.

更新

$("table").each(function() {
 var Bsum = 0;
  var BundelID = '';
$(this).find(".row@R_794_10586@l").each(function() {

  var RowID = $(this).attr('id');
  //var suffix = RowID.match(/\d+/)[0];
  var suffix = RowID.split("_")[1];

  console.log(suffiX)
  BundelID = $('.bundle' + suffiX).attr('id');
  console.log(BundelID);
  if (RowID.indexOf(BundelID) != -1) {
    var BValue = $('#' + RowID).val();
    if (!isNaN(Bvalue)) {
      Bsum += parseFloat(Bvalue);
    }
  }
  console.log(Bsum);
  $('.bundelRow@R_794_10586@l' + BundelID).html(parseFloat(Bsum).toFixed(2));

});
});

Here是一份工作副本注意 – >我从HTML中删除了Sums,数字由代码计算

大佬总结

以上是大佬教程为你收集整理的如何通过使用jquery添加该表的行来获取表总数全部内容,希望文章能够帮你解决如何通过使用jquery添加该表的行来获取表总数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。