效果图:

html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模拟购物车功能-jq</title>
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="css/shopCart.css" />
<script type="text/javascript" src="js/jquery-2.1.0.js" ></script>
</head>
<body> <table class="table">
<tr>
<th id="checkAll"><label><input type="checkbox" checked />全选</label><button>删除</button></th>
<th>商品名称</th>
<th>商品价格(元)</th>
<th>数目</th>
<th>小计(元)</th>
<th>操作</th>
</tr>
<tr>
<td class="check"><label><input type="checkbox" checked /></label></td>
<td>商品名称1</td>
<td class="price">22.50</td>
<td><span class="sub">-</span><input type="text" value="1" class="num"/><span class="add">+</span></td>
<td class="subtotal">22.50</td>
<td class="del"><button>删除</button></td>
</tr>
<tr>
<td class="check"><label><input type="checkbox" checked /></label></td>
<td>商品名称2</td>
<td class="price">12.50</td>
<td><span class="sub">-</span><input type="text" value="1" class="num"/><span class="add">+</span></td>
<td class="subtotal">12.50</td>
<td class="del"><button>删除</button></td>
</tr>
<tr>
<td class="check"><label><input type="checkbox" checked /></label></td>
<td>商品名称3</td>
<td class="price">110.40</td>
<td><span class="sub">-</span><input type="text" value="1" class="num"/><span class="add">+</span></td>
<td class="subtotal">110.40</td>
<td class="del"><button>删除</button></td>
</tr>
<tr>
<td colspan="5" style="text-align: right;">总件数:<i id="numAll">0</i>件 &nbsp; &nbsp; 总计:<i id="total">0.00</i>元</td>
</tr>
</table> <script src="js/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
<script src="js/shopCart.js" type="text/javascript" charset="utf-8"></script> </body>
</html>

css样式:

*{
margin:;
padding:;
} table th,table td,input{
text-align: center;
}
table #checkAll{
width: 150px;
}
table #checkAll label{
cursor: pointer;
background: url(../img/confirm.png) no-repeat center left;
padding-left:10px;
}
table .check label{
cursor: pointer;
background: url(../img/confirm.png) no-repeat center;
} table #checkAll input, table .check input{
visibility: hidden; } table input[type="text"]{
width: 50px;
overflow: hidden;
}
table span{
display: inline-block;
width: 20px;
background: #8C8C8C;
margin:0px 5px ;
color: #FFFFFF;
cursor: pointer;
}

js:

$(function() {

    // 全选
$("#checkAll input").click(function() {
var flag = $(this).prop("checked");
if(flag) {
$(".check label input").prop("checked", true); $("#checkAll label").css("background", "url(img/confirm.png) no-repeat center left");
$(".check label").css("background", "url(img/confirm.png) no-repeat center"); } else {
$(".check label input").prop("checked", false); $("#checkAll label").css("background", "url(img/confirm_no.png) no-repeat center left");
$(".check label").css("background", "url(img/confirm_no.png) no-repeat center");
}
counts();
totalPrice();
}); //单选
$(".check input").click(function() {
var flag = $(this).prop("checked"); //获取当前input的状态
var CL = $(".check input").length; //列表长度;
var CH = $(".check input:checked").length; //列表中被选中的长度 if(flag) {
$(this).parent("label").css("background", "url(img/confirm.png) no-repeat center");
} else {
$(this).parent("label").css("background", "url(img/confirm_no.png) no-repeat center");
} if(CL == CH) {
$("#checkAll input").prop("checked", true);
$("#checkAll label").css("background", "url(img/confirm.png) no-repeat center left");
} else {
$("#checkAll input").prop("checked", false);
$("#checkAll label").css("background", "url(img/confirm_no.png) no-repeat center left");
}
counts();
totalPrice();
}) //数目加
$(".add").click(function() {
var num = $(this).prev().val();
var price = parseFloat($(this).parent().siblings(".price").text());
num++;
$(this).prev().val(num); // 小计
$(this).parent().siblings(".subtotal").text((price * num).toFixed(2));
counts();
totalPrice();
}) //数目减
$(".sub").click(function() {
var num = $(this).next().val();
var price = parseFloat($(this).parent().siblings(".price").text());
num--; if(num <= 0) {
num = 0
}
$(this).next().val(num); // 小计
$(this).parent().siblings(".subtotal").text((price * num).toFixed(2));
counts();
totalPrice();
}) //文本框脱里焦点处理
$('.num').each(function(i) {
$(this).blur(function() {
let p = parseFloat($(this).parents('tr').find(".subtotal").text());
let c = parseFloat(this.value);
console.log(p*c);
$(this).parents('tr').find(".subtotal").text((c * p).toFixed(2));
counts();
totalPrice();
})
}) //单行删除
$(".del button").click(function() {
var flag = $(this).parent().siblings().find("input").prop("checked");
if(flag) {
if(confirm("是否确定删除")) {
$(this).parents("tr").remove();
var CL = $(".check input").length; //列表长度;
if(CL == 0) {
$("#checkAll label input").prop("checked", false);
$("#checkAll label").css("background", "url(img/confirm_no.png) no-repeat center left");
}
counts();
totalPrice();
}
}
}) //全删除
$("#checkAll button").click(function() {
var flag = $(this).prev().children().prop("checked");
// console.log(flag);
if(flag) { if(confirm("是否确定删除")) { $(".check").parent().remove();
var CL = $(".check input").length; //列表长度; if(CL == 0) {
$("#checkAll label input").prop("checked", false);
$("#checkAll label").css("background", "url(img/confirm_no.png) no-repeat center left");
}
counts();
totalPrice();
} }
}) //总价格
totalPrice(); function totalPrice() {
var prices = 0;
$('.check input:checked').each(function(i) {
console.log()
prices += parseFloat($(this).parents("tr").find('.subtotal').text());
})
$('#total').text(prices);
}
//总数目
counts(); function counts() {
var sum = 0;
$('.check input:checked').each(function(i) {
sum += parseInt($(this).parents("tr").find('.num').val());
})
$('#numAll').text(sum);
} })

购物车功能:使用jQuery实现购物车全选反选,单选,商品增删,小计等功能的更多相关文章

  1. jquery、js全选反选checkbox

    操作checkbox,全选反选 //全选 function checkAll() { $('input[name="TheID"]').attr("checked&quo ...

  2. Jquery 1.8全选反选删除选中项实现

    JQuery1.6以后,Prop的出现,让1.6以下的全选反选效果全部失效了.以下是修正后的版本: 全选反选效果: $(".checkbox").click(function () ...

  3. jQuery实现checkbox全选反选及删除等操作

    1.list.html 说明:用checkbox数组Check[]存放每一行的ID值 <div id="con"> <table width="100% ...

  4. JQuery一句话实现全选/反选

    $("#checkAll").click(function () { if (this.checked) {     $("input[name='checkbox']& ...

  5. jquery 书写全选反选功能

    书写一个后台管理中用到的全选反选功能.代码如下 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  6. jQuery实现的全选、反选和不选功能

    适用于网页多选后需要进行批量操作的场景(如批量删除等).如有问题希望大家可以指正.谢谢~~ HTML 我们的页面上有一个歌曲列表,列出多行歌曲名称,并匹配复选框供用户选择,并且在列表下方有一排操作按钮 ...

  7. jQuery实现的全选、反选和获取当前所有选中的值功能

    链接: jQuery实现的全选.反选和获取当前所有选中的值功能 <ul id="list"> <li><label><input type ...

  8. jquery实现全选/反选功能

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  9. JavaScript 实现全选 / 反选功能

    JavaScript 实现全选 / 反选功能 版权声明:未经授权,内容严禁转载! 构建主体界面 编写 HTML 代码 和 CSS 代码,设计主题界面 <style> #user { wid ...

  10. jQuery设置checkbox全选(区别jQuery版本)

    jQuery设置checkbox全选在网上有各种文章介绍,但是为什么在我们用他们的代码的时候就没有效果呢? 如果你的代码一点错误都没有,先不要急着怀疑人家代码的正确性,也许只是人家跟你用的jQuery ...

随机推荐

  1. EventTarge Node Docuement Element HTMLElement 关系

    综述: 可以将其看做是依次继承的关系: Node Node A Node is an interface from which a number of DOM types inherit, and a ...

  2. 通过Windows Server 2008 R2建立iSCSI存储

    名词解释:iSCSI技术是一种由IBM公司研究开发的,是一个供硬件设备使用的可以在IP协议的上层运行的SCSI指令集,这种指令集合可以实现在IP网络上运行 SCSI协议,使其能够在诸如高速千兆以太网上 ...

  3. linux man指令问题

    linux man指令问题 2010-1-13 13:33 提问者: 钟离伊轩 man命令执行时,可加入数值,来限制帮助级别. 这句话对不对啊???? 我记得man page是分章节的..好像可以加数 ...

  4. JS:二维数组排序和获取子级元素

    JS:二维数组排序和获取子级元素 1. 二维数组排序 1.按数值排序 var arr = [[1, 2, 3], [7, 2, 3], [3, 2, 3]]; 如果我们要按每个子数组的第一列来排序要如 ...

  5. 消息中间件--"rocketmq"02之QuickStart

    依赖 <dependency> <groupId>com.alibaba.rocketmq</groupId> <artifactId>rocketmq ...

  6. Python常用库之二:Pandas

    Pandas是用于数据操纵和分析,建立在Numpy之上的.Pandas为Python带来了两种新的数据结构:Pandas Series和Pandas DataFrame,借助这两种数据结构,我们能够轻 ...

  7. Kali-linux密码在线破解

    为了使用户能成功登录到目标系统,所以需要获取一个正确的密码.在Kali中,在线破解密码的工具很多,其中最常用的两款分别是Hydra和Medusa.本节将介绍使用Hydra和Medusa工具实现密码在线 ...

  8. Kali-linux系统指纹识别

    现在一些便携式计算机操作系统使用指纹识别来验证密码进行登录.指纹识别是识别系统的一个典型模式,包括指纹图像获取.处理.特征提取和对等模块.如果要做渗透测试,需要了解要渗透测试的操作系统的类型才可以.本 ...

  9. vector详讲(二)迭代器

    先看一下代码: #include <iostream> #include <vector> int main() { std::vector<double> dou ...

  10. Spring(十二)之JDBC框架

    JDBC 框架概述 在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQ ...