jQuery每个计数问题

jQuery每个计数问题,jquery,Jquery,我试图在每个人的“dt”包装内显示计数,但目前它在“dt”包装内外的每个人之间显示计数。我附上了一张丝网印刷品。如果有人能给我这个建议,我将不胜感激 $('.order_table_item .variation dt').each(function () { if ($(this).text() == 'Candidate Name:') { $(this).next().show(); $(this).before("<

我试图在每个人的“dt”包装内显示计数,但目前它在“dt”包装内外的每个人之间显示计数。我附上了一张丝网印刷品。如果有人能给我这个建议,我将不胜感激

 $('.order_table_item .variation dt').each(function () {
        if ($(this).text() == 'Candidate Name:') {
            $(this).next().show();
            $(this).before("<div class='count'></div>");
            $(this).next().after("<div class='clear'></div>");

        } else {
            $(this).hide();
        }
    });

    $('.variation .count').each(function(i) {
            $(this).append((i+1));
    });
现在它的数量翻了一番

我的html的已清理短接版本

  <table class="shop_table order_details">
    <tbody>
        <tr class="order_table_item">
            <td class="product-name"> 
                <dl class="variation">
                    <div class="count" style="border-top: 0px none;">11</div>
                    <dd style="display: block; border-top: 0px none;">Dave</dd>
                    <div class="clear"></div>
                </dl>
            </td>
        </tr>
        <tr class="order_table_item">
            <td class="product-name">
                <dl class="variation">
                    <div class="count">22</div>
                    <dd style="display: block;">JACK</dd>
                    <div class="clear"></div>
                    <div class="count">33</div>
                    <dd style="display: block;">JOHN</dd>
                </dl>
            </td>
        </tr>
    </tbody>
</table>

11
戴夫
22
杰克
33
厕所

由于我不知道您的代码是什么样子,我有三个想法:

想法1:

$('.variation .count').each(function(i) {
  $(this).text(i+1);
});
想法2:由于“1”+1=11,尝试解析i,使其成为int而不是字符串

$('.variation .count').each(function(i) {
  $(this).append(parseint(i)+1);
});
想法3:

$('.variation .count').each(function(index) { //i is already taken, better to use another variable here.
  $(this).text(index+1);
});
想法4:因为你在一个each循环中,我错过了这个循环,所以你可能想循环它的每一个。计数,而不是全部。每次都计数

$('.order_table_item').each(function(i,e){
  $(this).find('.variation .count').each(function(index) { //use $(this) to loop every .count inside .order_table_item.
    $(this).text(index+1);
  });
});

如果我理解正确:

演示:

JQuery 产出:

111戴夫

221杰克

332约翰


显示您的html代码以及$('.variation.count'),将其更改为$('.variation dt.count');在你的小提琴上,输出应该是:111、221、332吗?我尝试了所有这些,它们都输出相同的问题,要么重复数字,例如11、22、33,要么从一个div到另一个div,而不是从一个1、二个1、2、3。我已经添加了我的html上面,如果他们是任何其他你可以看到,请让我知道。
$('.order_table_item').each(function(i,e){
  $(this).find('.variation .count').each(function(index) { //use $(this) to loop every .count inside .order_table_item.
    $(this).text(index+1);
  });
});
$('.variation').each(function(x) {
    $(this).find('.count').each(function(i) {
        $(this).append(i+1);
    });
});