注意给checkbox定制设计定制设计添加事件就是用change()
给button添加事件就是用click()
1、每次点击+号,定制设计根据文本框的值乘以当定制设计前商品的价格就是商品的小计
2、定制设计只能增加本商品的小计,定制设计就是当前商品的小计模块
3、定制设计修改普通元素的内容是text方法
4、定制设计当前商品的价格要把¥定制设计符号去掉再相乘 substr()
5、parents(‘选择器’)定制设计可以返回指定祖先元素4
6、定制设计最后计算的结果如果想要通过toFixed(2)方法
7、定制设计用户也可以直接修改表定制设计单里面的值,定制设计同样要计算小计,用表单change事件
8、定制设计用最新的表单内容的值定制设计乘以单价即可,定制设计但还是当前商品的小计
定制设计计算总计和总额:
思路:定制设计把所有文本框里面的值定制设计相加就是总计数量,总额同理
文本框里面的值不相同,如果想要相加就需要用到each的遍历,声明一个变量,相加即可
点击+号或者-号,都会改变总计和总额,如果用户修改了文本框里面的值也会改变总额,那么都要分别添加到这三个事件中,因此封装一个函数来求总额和总计,以上几个操作调用即可
注意:总计是文本框中的值相加val()。总额是普通元素的内容text()
注意普通元素里面的内容要去掉¥并且转为数字型后才能相加
删除商品模块:
1、把商品remove()删除元素即可
2、三个地方需要删除:1、商品后面的删除按钮2、删除选中的商品3、清理购物车
1、商品后面的删除按钮:一定是删除当前的商品,所以从$(this)出发
2、删除选中的商品:先判断小的复选框按钮是否选中状态,如果选中,则删除对应的商品
可以不用遍历,$('element:checked')可以直接拿出被选中的复选框
选中商品添加背景色
核心:选中的商品添加背景,不选中的商品移除背景即可
1、全选按钮点击:如果全选是选中的,则所有的商品添加背景,否则移除背景
2、小的复选框点击:如果是选中状态,则当前商品添加背景,否则移除背景
3、这个背景,可以通过类进行修改,添加类和移除类
- <!DOCTYPE html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- img {
- width: 80px;
- height: 80px
- }
-
- a {
- color: black;
- text-decoration: none;
- }
-
- .mony {
- width: 20px;
- text-align: center;
- }
- </style>
- <script src="jquery.min.js"></script>
- </head>
-
- <body>
- <div>全部商品</div>
- <!-- 一个表单元素 -->
- <form action="">
- <table style="border-collapse: 0px;border-spacing: 0px;width:872px">
- <tr style="background-color:grey;">
- <td style='width:60px'><input type="checkbox" class="choseall">全选</td>
- <td style="width:472px;height:40px">商品</td>
- <td style='width:60px'>单价</td>
- <td style="width:100px">数量</td>
- <td style="width:110px">小计</td>
- <td style='width:70px'>操作</td>
- </tr>
- <tr>
- <td><input type="checkbox" class="goodchose"></td>
- <td>
- <div>
- <div style="float:left">
- <a href="">
- <img src="https://img.alicdn.com/bao/uploaded/i2/2904754564/O1CN011Q0OER1jaMJXxFeHl_!!2904754564.jpg_240x240.jpg" alt="">
- </a>
- </div>
- <div>
- <a href="javascript:;">【新势力周】YUKI小树家可爱趣味印花短款白T恤+卡扣高腰宽松复古阔腿牛仔长裤</a>
- </div>
- </div>
- </td>
- <td class="onemony">¥37.00</td>
- <td>
- <input type="button" value="-" class="jian"><input type="text" class="mony" value="1"><input type="button" value="+" class="add">
- </td>
- <td class="totalmony">¥37.00</td>
- <td class="deletthis">删除</td>
- </tr>
- <tr>
- <td><input type="checkbox" class="goodchose"></td>
- <td>
- <div>
- <div style="float:left">
- <a href="">
- <img src="https://img.alicdn.com/bao/uploaded/i1/3606826698/O1CN01NPCasY1zLjTBGmAax_!!3606826698.jpg_240x240.jpg" alt="">
- </a>
- </div>
- <div>
- <a href="javascript:;">日系正肩短袖t恤女夏薄款设计感小众日系甜酷学生小个子宽松上衣</a>
- </div>
- </div>
- </td>
- <td class="onemony">¥49.90</td>
- <td>
- <input type="button" value="-" class="jian"><input type="text" class="mony" value="1"><input type="button" value="+" class="add">
- </td>
- <td class="totalmony">¥49.90</td>
- <td class="deletthis">删除</td>
- </tr>
- <tr>
- <td><input type="checkbox" class="goodchose"></td>
- <td>
- <div>
- <div style="float:left">
- <a href="">
- <img src="https://img.alicdn.com/bao/uploaded/i3/3606826698/O1CN01jfI3hB1zLjZhqP5rC_!!3606826698.jpg_240x240.jpg" alt="">
- </a>
- </div>
- <div>
- <a href="javascript:;">春秋格子衬衫外套女2022新款小个子设计感小众宽松慵懒风日系上衣</a>
- </div>
- </div>
- </td>
- <td class="onemony">¥69.90</td>
- <td>
- <input type="button" value="-" class="jian"><input type="text" class="mony" value="1"><input type="button" value="+" class="add">
- </td>
- <td class="totalmony">¥69.90</td>
- <td class="deletthis">删除</td>
- </tr>
- <tr>
- <td><input type="checkbox" class="goodchose"></td>
- <td>
- <div>
- <div style="float:left">
- <a href="">
- <img src="https://img.alicdn.com/bao/uploaded/i2/628189716/O1CN01mpRMR22Ldyv4OgUtt_!!0-item_pic.jpg_80x80.jpg" alt="">
- </a>
- </div>
- <div>
- <a href="javascript:;">满减【百草味-红油面皮134g】方便面盒装速食凉皮拌泡面米粉凉皮</a>
- </div>
- </div>
- </td>
- <td class="onemony">¥23.70</td>
- <td>
- <input type="button" value="-" class="jian"><input type="text" class="mony" value="1"><input type="button" value="+" class="add">
- </td>
- <td class="totalmony">¥23.70</td>
- <td class="deletthis">删除</td>
- </tr>
- </table>
- <div style="position:fixed;bottom:0px;width:872px;background-color:rgb(184, 177, 177);height:50px;line-height: 50px;">
-
- <input type="checkbox" class="chosealllast">全选<span style="position:absolute;left:70px" class="chosedelet">删除选中的商品</span><span style="position:absolute;left:200px" class="clearall">清理购物车</span>
- <span style="position:absolute;right:230px;" class="totoalgoodn">已经选0件商品</span><span style="position:absolute;right:150px;">总价:</span>
- <span style="position:absolute;left:715px;color:red;font-weight: bold;" class="totalprice">¥12.60</span>
-
-
- </div>
- </form>
- <script>
- $(function() {
- //1、全选按钮。上下的全选按钮效果是一样的,按钮状态选中,那么所有商品的状态为选中。点击全选按钮让其不选中时,商品的选中状态就全部取消choseall与goodchose
- $('.choseall').change(function() {
- //让下面商品的选中状态与全选按钮的一致$('.choseall').prop(checked)
- $('.goodchose').prop('checked', $('.choseall').prop('checked'))
- $('.chosealllast').prop('checked', $('.choseall').prop('checked'))
- // //全选按钮按下,那么商品总数就等于goodchose的数量
- // if ($('.choseall').prop('checked') == true) {
- // $('.totoalgoodn').text('已经选' + $('.goodchose').length + '件商品')
- // }
- // //取消全选数目就等于0
- // if ($('.choseall').prop('checked') == false) {
- // $('.totoalgoodn').text('已经选' + 0 + '件商品')
- // }
- //复制一份到下面的全选按钮中
- if ($(this).prop('checked') == true) {
- $('.goodchose').parent().parent().css('background-color', 'aliceblue')
- } else {
- $('.goodchose').parent().parent().css('background-color', '')
- }
- })
- $('.chosealllast').change(function() {
- //让下面商品的选中状态与全选按钮的一致$('.choseall').prop(checked)
- $('.goodchose').prop('checked', $('.chosealllast').prop('checked'))
- $('.choseall').prop('checked', $('.chosealllast').prop('checked'))
- // if ($('.choseall').prop('checked') == true) {
- // $('.totoalgoodn').text('已经选' + $('.goodchose').length + '件商品')
- // }
- // //取消全选数目就等于0
- // if ($('.choseall').prop('checked') == false) {
- // $('.totoalgoodn').text('已经选' + 0 + '件商品')
- // }
- if ($(this).prop('checked') == true) {
- $('.goodchose').parent().parent().css('background-color', 'aliceblue')
- } else {
- $('.goodchose').parent().parent().css('background-color', '')
- }
- })
-
- //2、当商品的选中个数小于商品栏个数的时候,两个全选按钮就会取消选中element:checked 用来专门获取复选框被选中的情况.length获取个数
- $('.goodchose').change(function() {
- // console.log($('.goodchose:checked'))
- if ($('.goodchose:checked').length != $('.goodchose').length) {
- $('.choseall').prop('checked', false)
- $('.chosealllast').prop('checked', false)
-
- }
-
- //当商品的选中个数等于商品个数的时候,就相当于全选了,此时两个全选复选框全部选中
- if ($('.goodchose:checked').length == $('.goodchose').length) {
- $('.choseall').prop('checked', true)
- $('.chosealllast').prop('checked', true)
- }
- // //6、更改商品总数内容
- // $('.totoalgoodn').text('已经选' + $('.goodchose:checked').length + '件商品')
- // console.log($('.goodchose:checked').length);
-
- //11
- if ($(this).prop('checked') == true) {
- $(this).parent().parent().css('background-color', 'aliceblue')
- } else {
- $(this).parent().parent().css('background-color', '')
- }
-
- })
-
- //3、修改数量为jian和add添加事件
- $('.add').click(function() {
-
- //这个商品的价格改变,siblings('')
- //是在初始值上每点击一次就减一,但最小为一
- var b = $(this).siblings('.mony').prop('value')
- //这个b是一个字符型
- $(this).siblings('.mony').prop('value', parseInt(b) + 1)
- //4|
- var c = $(this).parent().siblings('.onemony').text().substr(1)
- // console.log(c)
- //更改小计里面的内容,显示两个小数.toFixed(2)
- $(this).parent().siblings('.totalmony').text('¥' + (c * (parseInt(b) + 1)).toFixed(2))
- totalnumber()
- })
- $('.jian').click(function() {
- //这个商品的价格改变,siblings('')
- //是在初始值上每点击一次就减一,但最小为一
- var a = parseInt($(this).siblings('.mony').prop('value'))
- if (a >= 2) {
- $(this).siblings('.mony').prop('value', a - 1)
- //4|
- var c = $(this).parent().siblings('.onemony').text().substr(1)
- // console.log(c)
- //更改小计里面的内容,显示两个小数.toFixed(2)
- $(this).parent().siblings('.totalmony').text('¥' + (c * (a - 1)).toFixed(2))
- }
- totalnumber()
- })
-
- //4、实现小计跟随数量变化,用数量的值*单价。单价去掉¥,得到数字,因为是跟随数量变化的,所以这一部分添加到点击事件中去,substr()
-
- //5、手动更改数量的时候,小计会发生改变.给mony添加change事件
- $('.mony').change(function() {
- //获得这里面的值赋值给一个变量
- var number = $(this).prop('value')
- //获取对应的单价
- var c = $(this).parent().siblings('.onemony').text().substr(1)
- //要保证合格值大于等于1
- if (number >= 1) {
- var b = number * c
- }
- //当number小于1时,默认为1
- if (number < 1) {
- $(this).prop('value', 1)
- number = 1
- var b = number * c
- }
- // alert(number)
- // console.log(number)
- console.log(b)
- $(this).parent().siblings('.totalmony').text('¥' + (b).toFixed(2))
- totalnumber()
- })
- //6、获得已经选择的商品个数,就是看商品栏的状态选择个数,再状态变化的事件中进行更改--不对,商品数目拿的是表单里面的数量综合,一旦表单里面的元素值发生改变的时候,就要重新计算以下商品的数目
-
- //7、更改价格,价格的话就是小计的累加。那么就需要对小计进行遍历,一旦小计发生改变,就需要遍历一次去改变总价
- //函数封装
- //总计
- function totalnumber() {
- var ton = 0
- $('.mony').each(function(index, ele) {
- // console.log(index);
- // console.log($('.mony').eq(index).val());直接拿ele这个元素里面的内容,不需要进行索引
- ton = ton + parseInt($(ele).val())
- })
-
- $('.totoalgoodn').text('已经选' + ton + '件商品')
- //总额,从小计里面获得,去掉¥,再遍历相加
- var mon = 0
- $('.totalmony').each(function(i, e) {
- //首先获取每个元素的text
-
- mon = mon + parseInt($(e).text().substr(1))
-
- })
- //把mon给总价后的数值
- $('.totalprice').text('¥' + mon.toFixed(2))
- }
- totalnumber() //打开这个页面,有默认的值存在,所以也要调用一次
-
- //8、按下删除键,就删除此时的商品
- $('.deletthis').click(function() {
- //删除我的父亲tr
- $(this).parent().remove()
- totalnumber()
- })
-
- //9、删除选中的商品
- $('.chosedelet').click(function() {
- //要获取我选中的商品是那些,并将其删除
- //对所有的goodchose进行遍历
- // $('.goodchose').each(function(index, ele) {
- // //判断如果当前元素ele的状态是true,则将该元素的父亲的父亲tr删除
- // if ($(ele).prop('checked') == true) {
- // $(ele).parent().parent().remove()
- // }
- // })
- //可以不用遍历
- $('.goodchose:checked').parent().parent().remove()
- totalnumber()
- })
-
- //10、按下清理购物车,就将所有的商品都清理掉
- $('.clearall').click(function() {
- //这里不需要进行遍历
- $('.goodchose').parent().parent().remove()
- totalnumber()
- })
- //11、选定一个商品就给其添加一个背景色,放到商品购物状态change事件中
-
- })
- </script>
- </body>
-
- </html>