【发布时间】:2020-02-17 03:08:45
【问题描述】:
我有产品列表,我想在选中复选框时计算它们的价格,目前我的代码将计算所有复选框,包括未选中的复选框。
JavaScript
<script>
$(document).ready(function () {
$(".bundlechecked").on('click', function () { //div checkbox
var pp = 0;
$(".thisPrice").each(function() { //input where price comes from
if(!isNaN(this.value) && this.value.length!=0)
{
pp += parseFloat(this.value);
}
});
console.log(pp);
});
});
</script>
Blade (View)
@foreach($bundles as $product)
<div class="bundlechecked checkbox"> // how we define which item is selected (bundlechecked)
<label><input type="checkbox" value="{{$product->id}}">
<div class="product-thumb">
<div class="image"><img src="{{url('/')}}/images/{{$product->imageOne}}" alt="{{$product->imageOne_alt}}" title="{{$product->title}}" class="img-responsive" /></div>
<div class="caption">
<h4>{{$product->title}}</h4>
@if(!empty($product->newprice))
<p class="price">
<span class="price-new">{{ __('frontend.rp') }} {{ number_format($product->newprice, 0) }}</span>
<input type="hidden" class="thisPrice" value="{{$product->newprice}}"> // getting price of selected item
<span class="price-old">{{ __('frontend.rp') }} {{ number_format($product->price, 0) }}</span>
<span class="saving">
- {{number_format(($product->price - $product->newprice) / $product->price * 100, 0) }}%
</span>
</p>
@else
<p class="price"> {{ __('frontend.rp') }} {{ number_format($product->price, 0) }} </p>
<input type="hidden" class="thisPrice" value="{{$product->price}}"> // getting price of selected item
@endif
</div>
</div>
</label>
</div>
@endforeach
我对代码进行了注释以便更好地理解
有什么想法吗?
【问题讨论】:
-
嗨@mafortis,我看到这个价格被隐藏了
-
@KrishnaJonnalagadda 是的
-
你想计算隐藏值吗?
-
我只是尝试获取在复选框中显示的产品的价格,因为我的价格显示在
<p class="price">标签中,我决定隐藏输入以获取这些值,如果有更好的方法为此,我没有任何反对意见,请随时分享您的想法。
标签: javascript php laravel-blade