html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="css.css">
<script src="js.js"></script>
</head>
<body>
<table border="1px" cellpadding="0" cellspacing="0">
<thead>
<th>序号</th>
<th>商品名称</th>
<th>数量</th>
<th>单价</th>
<th>小计</th>
<th>操作</th>
</thead>
<tbody id="tbody">
<tr>
<td class="xuhao"></td>
<td>烤煎饼</td>
<td>
<input type="button" value="-" onclick="count('-',this)">
<input type="number" value="3" class="num"/>
<input type="button" value="+" onclick="count('+',this)">
</td>
<td>单价:
<input type="number" value="2" class="price"/>
</td>
<td>小计:
<input type="number" class="sum"/>
</td>
<td><input type="button" value="删除" onclick="remove(this)"></td>
</tr>
<tr>
<td class="xuhao"></td>
<td>珍珠奶茶</td>
<td>
<input type="button" value="-" onclick="count('-',this)">
<input type="number" value="3" class="num"/>
<input type="button" value="+" onclick="count('+',this)">
</td>
<td>单价:
<input type="number" value="3.5" class="price"/>
</td>
<td>小计:
<input type="number" class="sum"/>
</td>
<td><input type="button" value="删除" onclick="remove(this)"></td>
</tr>
<tr>
<td class="xuhao"></td>
<td>羊肉串</td>
<td>
<input type="button" value="-" onclick="count('-',this)">
<input type="number" value="3" class="num"/>
<input type="button" value="+" onclick="count('+',this)">
</td>
<td>单价:
<input type="number" value="1.5" class="price"/>
</td>
<td>小计:
<input type="number" class="sum"/>
</td>
<td><input type="button" value="删除" onclick="remove(this)"></td>
</tr>
<tr>
<td class="xuhao"></td>
<td>牛头</td>
<td>
<input type="button" value="-" onclick="count('-',this)">
<input type="number" value="3" class="num"/>
<input type="button" value="+" onclick="count('+',this)">
</td>
<td>单价:
<input type="number" value="10" class="price"/>
</td>
<td>小计:
<input type="number" class="sum"/>
</td>
<td><input type="button" value="删除" onclick="remove(this)"></td>
</tr>
<tr>
<td class="xuhao"></td>
<td>烧刀子</td>
<td>
<input type="button" value="-" onclick="count('-',this)">
<input type="number" value="3" class="num"/>
<input type="button" value="+" onclick="count('+',this)">
</td>
<td>单价:
<input type="number" value="1.8" class="price"/>
</td>
<td>小计:
<input type="number" class="sum"/>
</td>
<td><input type="button" value="删除" onclick="remove(this)"></td>
</tr>
<tr>
<td class="xuhao"></td>
<td>花生米</td>
<td>
<input type="button" value="-" onclick="count('-',this)">
<input type="number" value="3" class="num"/>
<input type="button" value="+" onclick="count('+',this)">
</td>
<td>单价:
<input type="number" value="3" class="price"/>
</td>
<td>小计:
<input type="number" class="sum"/>
</td>
<td><input type="button" value="删除" onclick="remove(this)"></td>
</tr>
<tr>
<td colspan="6">
商品一共 <span id="sum_cou"></span> 件;共计花费 <span id="sum_price"></span> 元;其中最贵的商品单价是 <span
id="max_price"></span>元
</td>
</tr>
</tbody>
</table>
</body>
</html>
css代码
table {
text-align: center;
width: 1000px;
margin: 0 auto;
}
thead {
background-color: skyblue;
}
tr {
height: 40px;
}
form table {
text-align: left;
width: 100px;
}
input[type="number"] {
width: 50px;
border: 0;
text-align: center;
font-size: 18px;
}
js代码
function update_sum() {//更新最后一行总数,总价格,单价最大
var num = document.getElementsByClassName('num');
var price = document.getElementsByClassName('price');
var sum = document.getElementsByClassName('sum');
var sum_cou = document.getElementById('sum_cou');
var sum_price = document.getElementById('sum_price');
var max_price = document.getElementById('max_price');
var cou = 0, pri = 0, max_p = 0;
for (let i = 0; i < num.length; i++) {
sum[i].value = Number(num[i].value) * Number(price[i].value);
cou += Number(num[i].value);
pri += Number(sum[i].value);
if (max_p < Number(price[i].value)) {
max_p = Number(price[i].value);
}
}
sum_cou.innerHTML = cou;
sum_price.innerHTML = pri;
max_price.innerHTML = max_p;
}
function update_xuhao() {//更新序号
var xuhao = document.getElementsByClassName('xuhao');
for (let i = 0; i < xuhao.length; i++) {
xuhao[i].innerHTML = i + 1;
}
}
window.onload = function () {
update_sum();
update_xuhao();
};
function count(a, obj) {//数量的加减
var parentNode = obj.parentNode.parentNode;
var childNodes = parentNode.childNodes;
var num = childNodes[5].childNodes[3];
var price = childNodes[7].childNodes[1];
var sum = childNodes[9].childNodes[1];
var sum_cou = document.getElementById('sum_cou');
var sum_price = document.getElementById('sum_price');
var max_price = document.getElementById('max_price');
switch (a) {
case '+':
var b = Number(num.value);
num.value = b + 1;
sum.value = Number(num.value) * Number(price.value);
sum_cou.innerHTML = Number(sum_cou.innerHTML) + 1;
sum_price.innerHTML = Number(sum_price.innerHTML) + Number(price.value);//总价格加上单价
break;
case '-':
num.value -= 1;
sum.value = Number(num.value) * Number(price.value);
sum_cou.innerHTML = Number(sum_cou.innerHTML) - 1;//总数量减一
sum_price.innerHTML = Number(sum_price.innerHTML) + Number(price.value);//总价格减去单价
break;
}
}
function remove(obj) {//删除当前行
var tbody = document.getElementById('tbody');
if (confirm("确定要删除吗?")) {
tbody.removeChild(obj.parentNode.parentNode);
}
update_xuhao();
update_sum();
}
运行结果
当点击“+”或“-”,会进行小计以及下面一行的更新,当点击删除,会删除当前行,并对序号进行重新排序以及下面一行的总信息更新。。