javascript - 当数量增加导致价格上涨时
问题描述
在一张表中,我有产品及其数量和价格。我正在使用加号和减号按钮来增加或减少产品的数量。我也想根据数量增加和减少价格。谁能帮我?
<table>
<thead>
<tr>
<th scope="col">Company</th>
<th scope="col">Product</th>
<th width="200px">Quantity</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<tr>
<td id="Company"></td>
<td id="Product"></td>
<td>
<div class="input-group">
<span class="input-group-btn"><button class="btn btn-default value-control" data-
action="minus" data-target="font-size"><span class="glyphicon glyphicon-minus">
</span></button>
</span>
<input type="text" value="1" class="form-control" id="font-size">
<span class="input-group-btn"><button class="btn btn-default value-control" data-action="plus" data-target="font-size"><span class="glyphicon glyphicon-plus"></span></button>
</span>
</div>
</td>
<td id="Price"></td>
</tr>
</tbody>
</table>
$('#productSelect').change(function() {
var id = $(this).val();
if (id > 0) {
$.get("GetProduct", {
productId: id
}, function(result) {
console.log(result)
$("#Company").text(result.CompanyName);
$("#Product").text(result.ProductName);
$("#Quantity").text(result.ProductQuantity);
$("#Price").text(result.ProductPrice);
});
}
})
$(document).on('click', '.value-control', function() {
var action = $(this).attr('data-action')
var target = $(this).attr('data-target')
var value = parseFloat($('[id="' + target + '"]').val());
if (action == "plus") {
value++;
}
if (action == "minus") {
value--;
}
$('[id="' + target + '"]').val(value)
})
解决方案
这是我的代码
$(document).ready(function(){
var priceProduct = $("#priceProduct").attr('data-price');
var total = $("#priceProduct").attr('data-total');
var qty=1;
$(".quantity").click(function(){
if($(this).attr('data-action') == 'minus'){
if(qty == 1){
qty =1;
return false;
}else{
qty =qty-1;
total = parseInt(total) - parseInt(priceProduct);
}
}else{
qty =qty+1;
total = parseInt(priceProduct) + parseInt(total);
}
$("#quantity_input").val(qty);
$("#priceProduct").attr('data-total',total)
$("#Price").text("$"+total);
});
$("#Price").text("$"+total);
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<table>
<span id="priceProduct" data-price='10' data-total='10'>let suppose price would be $10</span>
<thead>
<tr>
<th scope="col">Company</th>
<th scope="col">Product</th>
<th width="200px">Quantity</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
<tr>
<td id="Company"></td>
<td id="Product"></td>
<td>
<div class="input-group ">
<span class="input-group-btn">
<button class="btn btn-default value-control quantity"
data-action="minus" data-target="font-size"><span class="glyphicon glyphicon-minus"></span>
</button>
</span>
<input type="text" value="1" class="form-control" id="quantity_input">
<span class="input-group-btn">
<button class="btn btn-default value-control quantity" data-action="plus" data-target="font-size">
<span class="glyphicon glyphicon-plus " ></span>
</button>
</span>
</div>
</td>
<td id="Price"></td>
</tr>
</tbody>
</table>
推荐阅读
- typescript - 测试时如何在异步 api 回调中处理 Promise.all
- linux - Linux:如果没有设置 SETUID 和 Capabilities,为什么我可以使用 ping?
- scala - 在表中为帐户添加上一个日期的新列
- flutter - Flutter - 在类中创建共享首选项的实例
- batch-file - 任务计划程序仅运行批处理脚本的一部分
- html - 如何让导航栏元素水平对齐
- java - 在 Eclipse PDE Manifest 中包含 Java Hex Editor
- html - 将用户重定向到与输入文本同名的页面的 HTML 文本框和按钮
- c# - ASP.NET 中的日期时间选择器格式
- python - 格兰杰的 MaxLag