<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-3.6.0.js"></script>
<style>
input[type=text] {
width: 30px;
}
</style>
<script>
$(function () {
$("input[type='text']").change(function () {
var n = $(this).val();
var price = $(this).siblings(".price").text().substring(1);
var total = (n * price).toFixed(2);
$(this).siblings(".count").text("¥" + total);
})
$(".increase").click(increaseFunc);
function updateLine(t, n) {
var price = t.siblings(".price").text().substring(1);
var sumPrice = n * price;
t.siblings(".count").text("¥" + sumPrice);
}
function getResult() {
}
function increaseFunc() {
var n = $(this).siblings("input").val();
n++;
$(this).siblings("input").val(n);
updateLine($(this), n);
}
$(".decrease").click(decreaseFunc);
function decreaseFunc() {
var n = $(this).siblings("input").val();
n--;
if (n < 1) {
alert("不能够再减了")
n = 1;
}
$(this).siblings("input").val(n);
updateLine($(this), n);
}
})
</script>
</head>
<body>
<ul class="car_list">
<li><span class="title">商品1</span><span class="price"></span>
<span class="price">¥10</span>
<button class="decrease">-</button>
<input type="text" value="1">
<button class="increase">+</button>
<span class="count">¥10</span>
</li>
<li><span class="title">商品2</span><span class="price"></span>
<span class="price">¥20</span>
<button class="decrease">-</button>
<input type="text" value="1">
<button class="increase">+</button>
<span class="count">¥20</span>
</li>
<li><span class="title">商品3</span><span class="price"></span>
<span class="price">¥30</span>
<button class="decrease">-</button>
<input type="text" value="1">
<button class="increase">+</button>
<span class="count">¥30</span>
</li>
</ul>
<p>小计:<span class="total" style="color: red">¥60</span></p>
</body>
</html>