
批改狀態(tài):合格
老師批語:
1.代碼部分
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>購物車</title>
<script src="jquery.js" type="text/javascript" charset="utf-8"></script>
<style>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
table{
width: 800px;
height: 400px;
border-bottom: 1px solid #000000;
border-top: 1px solid #000000;
margin: 10px auto;
text-align: center;
}
input[type="number"]{
width: 40px;
}
table,th {
background-color: lightgray;
border: none;
}
table , tr,td{
background-color: #fff;
border: none;
/* border-top:1px solid #000000; */
/* border-bottom:1px solid #000000; */
}
table , tr:last-of-type{
/* border-top:1px solid #000000; */
border-bottom:1px solid #000000;
}
table , tr:nth-last-of-type(2n) > td{
background-color: lightblue;
}
</style>
</head>
<body>
<table border="1" cellspacing="0">
<caption><h2>購物車 </h2></caption>
<tr>
<th><input type="checkbox" id="quan" checked="true"><label for="quan">全選</label></th>
<th>圖片</th>
<th>名稱</th>
<th>單位</th>
<th>價(jià)格</th>
<th>數(shù)量</th>
<th>總價(jià)</th>
</tr>
<tr>
<td><input type="checkbox" class='check' checked="true"></td>
<td>圖片</td>
<td>蘋果</td>
<td>臺</td>
<td class="price">4999</td>
<td><input type="number" value="1" min=1 max=10></td>
<td class="money">4999</td>
</tr>
<tr>
<td><input type="checkbox" class='check' checked="true"></td>
<td>圖片</td>
<td>蘋果</td>
<td>臺</td>
<td class="price">4999</td>
<td><input type="number" value="1" min=1 max=10></td>
<td class="money">4999</td>
</tr>
<tr>
<td><input type="checkbox" class='check' checked="true"></td>
<td>圖片</td>
<td>蘋果</td>
<td>臺</td>
<td class="price">4999</td>
<td><input type="number" value="1" min=1 max=10></td>
<td class="money">4999</td>
</tr>
<tr>
<td><input type="checkbox" class='check' checked="true"></td>
<td>圖片</td>
<td>蘋果</td>
<td>臺</td>
<td class="price">4999</td>
<td><input type="number" value="1" min=1 max=10></td>
<td class="money">4999</td>
</tr>
<tr>
<td><input type="checkbox" class='check' checked="true"></td>
<td>圖片</td>
<td>蘋果</td>
<td>臺</td>
<td class="price">4999</td>
<td><input type="number" value="2" min=1 max=10></td>
<td class="money">4999</td>
</tr>
<tr>
<td colspan="5">共計(jì)</td>
<td class="sum">5</td>
<td class="amount">1000000</td>
</tr>
</table>
</body>
<script type="text/javascript">
// 全選按鈕功能
$("#quan").change(ev=>$('.check').prop('checked',$(ev.target).prop('checked') ? true : false));
$('.check').change(()=>$("#quan").prop('checked',[...$('.check')].every(item=>$(item).prop('checked'))?true:false));
// 自動(dòng)計(jì)算商品價(jià)格和總價(jià)
autoComputer();
function autoComputer(){
let is=[...$(".check")].map(item=>$(item).prop("checked")?1:0);
let prices=[...$(".price")].map(item=>$(item).text()*1);
let nums=[...$("input[type='number']")].map(item=>$(item).val()*1);
let isnums=[nums,is].reduce((prev,curr)=>prev.map((item,index)=>item*curr[index]));
let moneys=[prices,nums].reduce((prev,curr)=>prev.map((item,index)=>item*curr[index]));
let ismoneys=[moneys,is].reduce((prev,curr)=>prev.map((item,index)=>item*curr[index]))
$(".money").each((index,item)=>$(item).text(moneys[index]));
$(".amount").text(ismoneys.reduce((prev,curr)=>prev+curr));
$(".sum").text(isnums.reduce((prev,curr)=>prev+curr));
}
$("input[type='number']").change(()=>autoComputer());
// 根據(jù)全選按鈕計(jì)算數(shù)量和總價(jià)
// let is=[...$(".check")].map(item=>$(item).prop("checked")?1:0);
$("table input[type='checkbox']").change(()=>autoComputer());
</script>
</html>
2.代碼運(yùn)行結(jié)果
3.案例分析:
1.全選按鈕功能:
(1)全選觸發(fā),主要監(jiān)聽全選按鈕的值,如果為true,就通過遍歷各個(gè)商品的選中按鈕設(shè)置為true;取消全選方法相同;而通過監(jiān)聽各個(gè)商品的選中按鈕的值,經(jīng)過數(shù)組函數(shù)every()判斷來設(shè)置全選按鈕是否為全選
(2)調(diào)整商品數(shù)量自動(dòng)計(jì)算商品價(jià)格:主要通過監(jiān)聽各個(gè)商品數(shù)量的變化觸發(fā)自動(dòng)計(jì)算函數(shù)來重新渲染出結(jié)果數(shù)據(jù);
(3)未選中產(chǎn)品不參與最終結(jié)算(而單個(gè)商品價(jià)格還的計(jì)算):通過遍歷商品選中按鈕的值返回一個(gè)數(shù)組(有0和1組成);在計(jì)算商品數(shù)量和計(jì)算商品總價(jià)時(shí),根據(jù)是否選中的值(1或0)來計(jì)算:如果為0時(shí)(未選中)在計(jì)算總數(shù)量時(shí),商品數(shù)量0參與計(jì)算,選中時(shí)商品數(shù)量1參與計(jì)算;計(jì)算總價(jià)原理相同
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號