【问题标题】:JavaScript calculate selected checkbox onlyJavaScript 仅计算选中的复选框
【发布时间】: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 是的
  • 你想计算隐藏值吗?
  • 我只是尝试获取在复选框中显示的产品的价格,因为我的价格显示在&lt;p class="price"&gt; 标签中,我决定隐藏输入以获取这些值,如果有更好的方法为此,我没有任何反对意见,请随时分享您的想法。

标签: javascript php laravel-blade


【解决方案1】:

试试这个

  • onchange 事件更适合此目的
  • 关键是只获取那些被检查的价格节点

var price = 0;
const calculatePrice = function(e) {
  let val = $(e).siblings().first().find('.caption .thisPrice').first().val();
  if(!isNaN(val) && val.length != 0){
      price += parseInt(val);
  }  
};

$(document).ready(function(){
  $('.bundlechecked').on('change', function(e){
    price = 0;
    $('.bundlechecked:checked').each((i, e, s) => calculatePrice(e));
    console.clear();
    console.log('Total price =>', price);
  });  
});
label { display: block;}
label * { display: inline;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>
  <input type="checkbox" class="bundlechecked">
  <div class="product-thumb">                
      <div class="caption">
        <h4>Product One - 10</h4>
        <input type="hidden" class="thisPrice" value="10">
      </div>
  </div>
</label>
<label>
  <input type="checkbox" class="bundlechecked">
  <div class="product-thumb">                
      <div class="caption">
        <h4>Product Two - 25</h4>
        <input type="hidden" class="thisPrice" value="25">
      </div>
  </div>
</label>
<label>
  <input type="checkbox" class="bundlechecked">
  <div class="product-thumb">                
      <div class="caption">
        <h4>Product Three - 12</h4>
        <input type="hidden" class="thisPrice" value="12">
      </div>
  </div>
</label>
<label>
  <input type="checkbox" class="bundlechecked">
  <div class="product-thumb">                
      <div class="caption">
        <h4>Product Four - 30</h4>
        <input type="hidden" class="thisPrice" value="30">
      </div>
  </div>
</label>
<label>
  <input type="checkbox" class="bundlechecked">
  <div class="product-thumb">                
      <div class="caption">
        <h4>Product Five - 15</h4>
        <input type="hidden" class="thisPrice" value="15">
      </div>
  </div>
</label>

【讨论】:

    【解决方案2】:

    试试这个:

        <script>
            $(document).ready(function () {
                $(".bundlechecked").on('click', function () { //div checkbox
                    var pp = 0;
                    $(".thisPrice").each(function() { //input where price comes from
                         var checkbox = $(this).parent("label").find("input[type=checkbox]");
                         if (checkbox.prop('checked') && !isNaN(this.value) && this.value.length!=0)
                         {
                             pp = +pp + +parseFloat($(this).val());
                         }
                    });
                    console.log(pp);
                });
            });
        </script>
    

    这里是 JSFiddle:https://jsfiddle.net/leonenco/8ojdesz6/26/

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-10
    • 2020-03-01
    • 2021-10-07
    • 1970-01-01
    • 2012-06-10
    • 2012-01-20
    • 2023-03-30
    • 2016-06-29
    相关资源
    最近更新 更多