我有一个优惠券系统,每当创建新的订单详细信息时,它都会将优惠券应用到标记为coupon_value的字段中。我正在尝试引入分层优惠券系统的概念,并试图找出是否有某种方法可以确定您使用的是哪个.each()实例。
这是我的代码:
//Default value of the coupon is $200
coupon_value = 200;
//If there are 5 students or more the coupon value is $300
teir_value = 300;
//For each coupon_value set its html to the coupon value variable
$('.coupon_value').each(function(){
$(this).html('$' + coupon_value));
});以下是我想要做的事情:
学生折扣:$200学生2折扣:$200学生3折扣:$200学生4折扣:$200学生5折扣:$300学生6折扣$300 .
如果这是呈现上的类优惠券值的第5个或更大的实例,那么可以在.each中使用coupon_value = $300或只使用tier_value吗?
发布于 2014-03-06 16:07:43
您可以使用当前项的索引来检查它是否是第五项或更多项。each()回调作为2个参数获取当前项的索引和当前项的索引。
//Default value of the coupon is $200
coupon_value = 200;
//If there are 5 students or more the coupon value is $300
teir_value = 300;
//For each coupon_value set its html to the coupon value variable
$('.coupon_value').each(function (i) {
$(this).html('$' + (i > 3 ? teir_value : coupon_value));
});演示:小提琴
正如您所使用的,如果要引用循环中的当前项,可以在每个处理程序中使用this。
https://stackoverflow.com/questions/22229761
复制相似问题