我使用 .each 来计算另一个 div 内的 div,但我有多个具有相同类的 div,并且我想为每个新的 div 包装器从 0 开始计数。
抱歉,这有点难以解释,所以这里有一个 jsFiddle 工作的链接:https://jsfiddle.net/1nds1put/
1.) 点击“添加季节性价格过滤器”
2.) 点击“添加价格规则”,您会看到它显示“价格规则#1”(工作正常)
3.) 再次点击“添加价格规则”,您会看到它显示“价格规则#2”(仍然正常工作)
4.) 再次点击“添加季节性价格过滤器”
5.) 在新的季节性价格过滤器中点击“添加价格规则”,您会看到它显示“价格规则#3”(不起作用,我希望它显示“价格规则#1”,因为它位于不同的div)
这是计算数字/计数器的函数:
function recacluclate() {
$( ".price-rule-wrapper-outer").children('div').each(function(index) {
$(this).find('.price-rule-number span').text(index + 1);
});
$( ".seasonal-filter-wrapper-outer").children('div').each(function(index) {
$(this).find('.seasonal-filter-number span').text(index + 1);
});
$( ".seasonal-filter-wrapper-outer .price-rule-wrapper-outer").children('div').each(function(index) {
$(this).find('.price-rule-number span').text(index + 1);
});
$( ".price-rule-wrapper-outer").children('div').each(function(index) {
$(this).find('.standard-adult-weekday').attr('name', 'standard-adult-weekday-' + (index + 1));
$(this).find('.standard-adult-weekend').attr('name', 'standard-adult-weekend-' + (index + 1));
$(this).find('.standard-child-weekday').attr('name', 'standard-child-weekday-' + (index + 1));
$(this).find('.standard-child-weekend').attr('name', 'standard-child-weekend-' + (index + 1));
});
}
非常感谢任何帮助!
最佳答案
替换
$( ".seasonal-filter-wrapper-outer .price-rule-wrapper-outer").children('div').each(function(index) {
$(this).find('.price-rule-number span').text(index + 1);
});
与
$(".seasonal-filter-wrapper-outer .price-rule-wrapper-outer").each(function() {
$(this).children('div').each(function(index) {
$(this).find('.price-rule-number span').text(index + 1);
});
})
关于javascript - jQuery 计算特定 div 中的 div,然后在添加新 div 时重置计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45277746/