我无法设定单位数量所需的价格

问题描述 投票:0回答:1
$.ajax({
  dataType: "json",
  url: "https://private-70cb45-aobara.apiary-mock.com/product/list",
  success: data =>{
    const price = data[0].unitPriceInCents;
    var cant = 1;
    //PRICE
    $("#Product-Price").text("$ " + data[0].unitPriceInCents/100);

    //ADD
    $("#Add").click(()=>{
        cant++
        $("#Product-Cant").text(cant);
        data[0].unitPriceInCents = data[0].unitPriceInCents + precio;
        $("#Product-Price").text("$ " + data[0].unitPriceInCents/100);
    });

    $("#Sustract").click(()=>{
        cant--
        $("#Product-Cant").text(cant);
        if (cant < 1) {cant=1;data[0].unitPriceInCents =data[0].unitPriceInCents;}
        data[0].unitPriceInCents = data[0].unitPriceInCents - price;
        $("#Product-Price").text("$ " + data[0].unitPriceInCents/100);
    });
  }
});

当单位数小于0时,价格应该是0,但它是负数。价格是美分,这是“/ 100”的原因

javascript jquery json ajax
1个回答
0
投票

var cant = 0;
var price = 0;
var data;
$.ajax({
  dataType: "json",
  url: "https://private-70cb45-aobara.apiary-mock.com/product/list",
  success: data => {
    price = data[0].unitPriceInCents;
    cant = 1;
    
    $("#Product-Cant").text(cant);
    //PRICE
    $("#Product-Price").text("$ " + ((price * cant) / 100));
  }
});


//ADD
$("#Add").click(() => {
      cant++;
      $("#Product-Cant").text(cant);
      $("#Product-Price").text("$ " + ((price * cant) / 100));
      });

    $("#Sustract").click(() => {
      cant--;

      if (cant < 1) {
        cant = 1;            
      }
      $("#Product-Cant").text(cant);
      $("#Product-Price").text("$ " + ((price * cant) / 100));
      
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Product-Price">

</div>

<div id="Product-Cant">
  
</div>
<a id="Add" href="#">
Add
</a><br>
<a id="Sustract" href="#">
Sustract
</a>
© www.soinside.com 2019 - 2024. All rights reserved.