$.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”
的原因答案 0 :(得分: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));
});
&#13;
<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>
&#13;