变量在循环结束时不递增

时间:2016-09-30 15:08:09

标签: javascript jquery

我有一个循环某些单元格并执行一些计算的函数。基本上我将一个值设置为一个变量作为一个起点......并且该数字假设在它遍历每个单元格后递增。数学和一切都按照需要工作,但变量不是递增,这使得一切都错了。它只捕获第一个值(在我下面的示例中为5.00)。这是我的功能:

$(function percentSpace() {
  var theIndexer = 20;
  var x = $('#rackPlan0 > tbody > tr.categorySub > td:not(:first-child,:last-child)');
  x.each(function() {
    var a = $('#rackPlan1 > tbody > tr:last-child > td:nth-child(' + theIndexer + ')');
    var b = $(this);
    var c = parseFloat(a.text(), 10) || 0;
    console.log(theIndexer);
    var d = parseFloat(b.text(), 10) || 0;
    var perc = parseFloat((d / c) * 100);
    $(this).html(perc.toFixed(0)).append('%');
  });
  theIndexer++;
})

theIndexer没有递增......不知道为什么在这个时刻...

这是完整示例的JSFIDDLE(请务必向右滚动以查看其他表格)。任何帮助都会非常感激!

2 个答案:

答案 0 :(得分:1)

您必须在each()回调中移动增量行,否则直到该迭代器之后才会执行。见下面的例子:

 $(function percentSpace() {
      var theIndexer = 20;
      var x = $('#rackPlan0 > tbody > tr.categorySub > td:not(:first-child,:last-child)');
      x.each(function() {
        var a = $('#rackPlan1 > tbody > tr:last-child > td:nth-child(' + theIndexer + ')');
        var b = $(this);
        var c = parseFloat(a.text(), 10) || 0;
        console.log(theIndexer);
        var d = parseFloat(b.text(), 10) || 0;
        var perc = parseFloat((d / c) * 100);
        $(this).html(perc.toFixed(0)).append('%');
      theIndexer++;
      });
      //output: indexer: 22
      console.log('indexer: ',theIndexer);
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table-style" id="rackPlan1">
  <thead>
    <tr class="nukeMe">
      <th>Category Group</th>
      <th>Category</th>
      <th>Proto S</th>
      <th>Fixture Points</th>
    </tr>
  </thead>
  <tbody>
    <tr class="nukeMe" style="display: none;">
      <td class="catGrp">SEASONAL</td>
      <td class="category">HEAD AND HAND (D23)</td>
      <td class="protoS">0.50</td>
      <td class="totalFixP">567</td>
    </tr>
    <tr class="nukeMe" style="display: none;">
      <td class="catGrp">SEASONAL</td>
      <td class="category">HEAD AND HAND (D23)</td>
      
      <td class="protoS">0.50</td>
      <td class="totalFixP">2,666</td>
    </tr>
    <tr class="fineLineSub">
      <td class="sub0 subTotal">SEASONAL</td>
      <td class="subTotal">HEAD AND HAND (D23)</td>
      <td class="subTotal">0.50</td>
      <td class="flFixP">3,233</td>
    </tr>
    <tr class="categorySub">
      <td class="catSub">SEASONAL</td>
      <td class="catSub">HEAD AND HAND (D23) TOTAL</td>
      <td class="catSub">0.50</td>
      <td class="catFixP">3,233</td>
    </tr>
    <tr class="nukeMe" style="display: none;">
      <td class="catGrp">SEASONAL</td>
      <td class="category">SWIMWEAR (D23)</td>
      <td class="protoS">0.00</td>
      <td class="totalFixP">582</td>
    </tr>
    <tr class="nukeMe" style="display: none;">
      <td class="catGrp">SEASONAL</td>
      <td class="category">SWIMWEAR (D23)</td>
      <td class="protoS">0.00</td>
      <td class="totalFixP">2,793</td>
    </tr>
    <tr class="fineLineSub">
      <td class="sub0 subTotal">SEASONAL</td>
      <td class="subTotal">SWIMWEAR (D23)</td>
      <td class="subTotal">0.00</td>
      <td class="flFixP">3,375</td>
    </tr>
    <tr class="nukeMe" style="display: none;">
      <td class="catGrp">SEASONAL</td>
      <td class="category">SWIMWEAR (D23)</td>
      <td class="protoS">0.50</td>
      <td class="totalFixP">588</td>
    </tr>
    <tr class="nukeMe" style="display: none;">
      <td class="catGrp">SEASONAL</td>
      <td class="category">SWIMWEAR (D23)</td>
      <td class="protoS">0.50</td>
      <td class="totalFixP">2,755</td>
    </tr>
    <tr class="fineLineSub">
      <td class="sub0 subTotal">SEASONAL</td>
      <td class="subTotal">SWIMWEAR (D23)</td>
      <td class="subTotal">0.50</td>
      <td class="flFixP">3,343</td>
    </tr>
    <tr class="nukeMe" style="display: none;">
      <td class="catGrp">SEASONAL</td>
      <td class="category">SWIMWEAR (D23)</td>
      <td class="protoS">1.00</td>
      <td class="totalFixP">587</td>
    </tr>
    <tr class="nukeMe" style="display: none;">
      <td class="catGrp">SEASONAL</td>
      <td class="category">SWIMWEAR (D23)</td>
      <td class="protoS">1.00</td>
      <td class="totalFixP">2,766</td>
    </tr>
    <tr class="fineLineSub">
      <td class="sub0 subTotal">SEASONAL</td>
      <td class="subTotal">SWIMWEAR (D23)</td>
      <td class="subTotal">1.00</td>
      <td class="flFixP">3,353</td>
    </tr>
    <tr class="nukeMe" style="display: none;">
      <td class="catGrp">SEASONAL</td>
      <td class="category">SWIMWEAR (D23)</td>
      <td class="protoS">0.00</td>
      <td class="totalFixP">537</td>
    </tr>
    <tr class="nukeMe" style="display: none;">
      <td class="catGrp">SEASONAL</td>
      <td class="category">SWIMWEAR (D23)</td>
      <td class="protoS">0.00</td>
      <td class="totalFixP">2,594</td>
    </tr>
    <tr class="fineLineSub">
      <td class="sub0 subTotal">SEASONAL</td>
      <td class="subTotal">SWIMWEAR (D23)</td>
      <td class="subTotal">0.00</td>
      <td class="flFixP">3,131</td>
    </tr>
    <tr class="categorySub">
      <td class="catSub">SEASONAL</td>
      <td class="catSub">SWIMWEAR (D23) TOTAL</td>
      <td class="catSub">1.50</td>
      <td class="catFixP">13,202</td>
    </tr>
    <tr class="totalRacks">
      <td class="catGrp">GRAND TOTAL</td>
      <td class="category"></td>
      <td class="totalColumn">2.00</td>
      <td class="totalFixP">16,435</td>
    </tr>
  </tbody>
</table>
<br />
<table class="table-style" id="rackPlan0">
  <thead>
    <tr>
      <th>Store Trait</th>
      <th>Proto A</th>
      <th>Proto S</th>
      <th>Total</th>
    </tr>
  </thead>
  <tbody>
    <tr class="storeCount">
      <td>Store Ct</td>
      <td class="protoA">5</td>
      <td class="protoS">3</td>
      <td class="totalRow">3,873</td>
    </tr>
    <tr class="runningStore">
      <td class="totalText">Running Store Ct</td>
      <td>5</td>
      <td>3,873</td>
      <td class="totalRow">3,873</td>
    </tr>
    <tr class="allocatedRacks">
      <td>Allocated Racks</td>
      <td class="protoA">10</td>
      <td class="protoS">1</td>
      <td class="totalRow">106</td>
    </tr>
    <tr class="overUnder">
      <td>Over/Under</td>
      <td>-5</td>
      <td>-5</td>
      <td class="catSub">0.50</td>
      <td class="catFixP">20%</td>
    </tr>
    <tr class="categorySub">
      <td class="catSub">SWIMWEAR (D23) %SPACE</td>
      <td class="catSub">4.00</td>
      <td class="catSub">1.50</td>
      <td class="catFixP">80%</td>
    </tr>
  </tbody>
</table>

答案 1 :(得分:0)

这最终为我做了工作......谢谢大家......

   $(function percentSpace() {
                var theIndexer = 20;
                var x = $('#rackPlan0 > tbody > tr.categorySub > td:not(:first-child)');
                x.each(function () {    
                    var a = $('#rackPlan1 > tbody > tr:last-child > td:nth-child(' + theIndexer + ')');
                    var b = $(this);
                    var c = parseFloat(a.text(), 10) || 0;                        
                    var d = parseFloat(b.text(), 10) || 0;
                    var perc = parseFloat((d / c) * 100);
                    if ($(this).hasClass('catSub')) {
                        $(this).html(perc.toFixed(0)).append('%');
                        theIndexer++;
                    }
                    else if ($(this).hasClass('catFixP')) {
                        theIndexer = 20;
                    }
                });                    
            })