繁体   English   中英

取消选中框并修改总计-JS

[英]Unchecking box and modifying grand total - JS

我正尝试在撤消物品时调高价格。 当前,当我删除一个项目时,我将grand total to 0设置grand total to 0

如果我有

benz - 150

bmw -  100  //with quantity of 2 meaning bmw price is 50

total  - 250

但是当我从列表中删除宝马时,

我应该得到这样的东西

benz - 150

total - 150

取消选择商品时,如何通过将总价反转为之前的价格来实现此目的?

 function calc(goods) { var product = JSON.parse(goods.dataset.goods); if (goods.checked == true) { $('.panel').append( '<div class="container"> ' + '<p class="name" >' + product.name + '</p>' + '<p class="price" data-price="' + product.price + '">' + product.price + '</p>' + '<p class="total" ><span class="sub-total" name="sub-total" id="sub-total"></span></p>' + '<input type="text" class="form-control quantity" placeholder=" qty " name="quantity[]" required/>' + '</div>' ) } else { $(".panel .container [data-id=" + product.id + "]").parent().remove(); $('.mygoods span').text(0); } } $('.panel').on('keyup', '.quantity', function() { var container = $(this).closest('div'); var quantity = Number($(this).val()); var price = Number($(this).closest('div').find('.price').data('price')); container.find(".total span").text(quantity * price); sum = 0; $(".sub-total").each(function() { sum = sum + Number($(this).text()); }) $('.mygoods span').text(sum); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <p class="mygoods">GrandTotal <span></span></p> </div> <input onclick="return calc(this)" data-good="{{$good->toJson()}}" type="checkbox" /> 

当我取消选择一个项目时,即使我加总了更新 ,旧文本也始终为0

 function calc(goods) {

  var product = JSON.parse(goods.dataset.goods);
  if (goods.checked == true) {
    $('.panel').append(
      '<div class="container"> ' +
      '<p  class="name" >' + product.name + '</p>' +
      '<p  class="price" data-price="' + product.price + '">' + product.price + '</p>' +
      '<p class="total" ><span class="sub-total" name="sub-total" id="sub-total"></span></p>' +
      '<input  type="text" class="form-control quantity"  placeholder=" qty " name="quantity[]"  required/>' +
      '</div>'
    )
  } else {
    var total = $(".panel .container [data-id=" + product.id + "]").parent().find(".total").text();

    $(".panel .container [data-id=" + product.id + "]").parent().remove();

 if (total) {
        $('.mygoods span').text(function(oldtext) {
            console.log('this is my old text '+oldtext)
            return oldtext ? oldtext - total : oldtext;

        });
    }

  }
}

在删除带有该产品小计的元素之前,请获取.total span元素的文本,然后从总计中减去。

  } else {
    var total = $(".panel .container [data-id=" + product.id + "]").parent().find(".total").text();
    $(".panel .container [data-id=" + product.id + "]").parent().remove();
    if (total) {
        $('.mygoods span').text(function(oldtext) {
            return oldtext ? oldtext - total : oldtext;
        });
    }
  }

纯js方法;

 window.onload = assignEventListener(); function assignEventListener() { let allCheckBoxes = document.querySelectorAll("input[type='checkbox']"); let grandTotal = 0; for(i=0;i<allCheckBoxes.length;i++) { allCheckBoxes[i].onchange = function() { checkedBoxesSum(allCheckBoxes,grandTotal); } } } function checkedBoxesSum(allCheckBoxes,grandTotal) { //let allChecked = document.querySelectorAll("input[type='checkbox']"); for(i=0;i<allCheckBoxes.length;i++) { if(allCheckBoxes[i].checked == true) { grandTotal += parseInt(allCheckBoxes[i].getAttribute('value')); } } document.getElementById("Grandtotal").innerHTML = "GrandTotal is "+ grandTotal } 
 <input type="checkbox" id="bmw" value="150"/> BMW <input type="checkbox" id="mercedes" value="300"/> Mercedes <input type="checkbox" id="audi" value="400"/> Audi <div id="Grandtotal"> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM