jQuery realizes the full function of shopping cart

jQuery realizes the full function of shopping cart

This article shares the specific code of jQuery to realize the full function of the shopping cart for your reference. The specific content is as follows

Effect picture:

HTML&&CSS:

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <script src="../jquery-3.4.1.min.js"></script>
 <style>
  * {
   margin: 0;
   padding: 0;
  }
  
  .tab {
   width: 500px;
   border-collapse: collapse;
   margin: 0 auto;
  }
  
  .tab td,
  th {
   border: 1px solid #000;
  }
  
  .tab .num {
   width: 20px;
  }
  
  .tab .add,
  .sub {
   width: 20px;
  }
  
  .current {
   background-color: pink;
  }
 </style>
</head>

<body>
 <table class="tab">
  <thead>
   <th>Select All<input type="checkbox" name="" value="" class="checkAll">
    <input type="checkbox" name="" value="" class="checkAll">
   </th>
   <th>Product Name</th>
   <th>Unit Price</th>
   <th>Quantity</th>
   <th>Subtotal</th>
   <th>Operation</th>
  </thead>
  <tbody>
   <tr>
    <td><input type="checkbox" class="ed" /></td>
    <td>Computer</td>
    <td class="price">¥200.20</td>
    <td>
     <button type="button" class="sub">-</button>
     <input type="text" name="" value="1" class="num">
     <button type="button" class="add">+</button>
    </td>
    <td class="small_total">¥200.20</td>
    <td class="delete">Delete</td>
   </tr>
   <tr>
    <td><input type="checkbox" class="ed" /></td>
    <td>Mobile phone</td>
    <td class="price">¥100.30</td>
    <td>
     <button type="button" class="sub">-</button>
     <input type="text" name="" value="1" class="num">
     <button type="button" class="add">+</button>
    </td>
    <td class="small_total">¥100.30</td>
    <td class="delete">Delete</td>
   </tr>
   <tr>
    <td><input type="checkbox" class="ed" /></td>
    <td>Air conditioning</td>
    <td class="price">¥1000.99</td>
    <td>
     <button type="button" class="sub">-</button>
     <input type="text" name="" value="1" class="num">
     <button type="button" class="add">+</button>
    </td>
    <td class="small_total">¥1000.99</td>
    <td class="delete">Delete</td>
   </tr>
  </tbody>
 </table>
 <div>
  <span><span style="color: red;" class="num_sum">1</span> item selected</span>
  <span>Total:</span>
  <span class="sum" style="color: red;">0</span>
  <div><span style="color: red;" class="delSome">Delete the selected product</span>
   <span style="color: red;" class="delAll">Empty shopping cart</span>
  </div>
 </div>
 </body>

</html>

JS:

//The selected state of the three small check buttons inside follows the Select All button //Because checked is an inherent property of the checkbox, use prop() to get and set this property $(function() {
   getSum();
   $(".checkAll").change(function() {
     // console.log($(this).prop("checked"));//The status of the select all button$(".ed,.checkAll").prop("checked", $(this).prop("checked"));
     getSum();
     if ($(".ed,.checkAll").prop("checked")) {
      //If all are selected, add the class name (background color) to all products
      $(".tab tbody").children().addClass("current");
     } else {
      $(".tab tbody").children().removeClass("current");
     }
    })
    //If all the small buttons are selected, the Select All button is selected. If the small buttons are not selected, the Select All button is not selected. //: checked selector, find the selected form element $(".ed").change(function() {
    // console.log($(".ed:checked").length);//Number of small checkboxes checked// console.log($(".ed").length);
    //console.log($(this).prop("checked"));
    if ($(".ed:checked").length === $(".ed").length) {
     $(".checkAll").prop("checked", true);
    } else {
     $(".checkAll").prop("checked", false);
    }
    getSum();
    if ($(this).prop("checked")) {
     $(this).parents("tr").addClass("current");
    } else {
     $(this).parents("tr").removeClass("current");
    }
   })

   $(".add").click(function() {
    let n = parseInt($(this).siblings(".num").val());
    //console.log(n);
    n++;
    $(this).siblings(".num").val(n);
    let price = $(this).parent().siblings(".price").html();
    price = price.substr(1);
    //console.log(price);
    $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
    getSum();
   })
   $(".sub").click(function() {
     let n = parseInt($(this).siblings(".num").val());
     //console.log(n);
     if (n === 1) {
      return false;
     }
     n--;
     $(this).siblings(".num").val(n);
     let price = $(this).parent().siblings(".price").html();
     price = price.substr(1);
     //console.log(price);
     $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
     getSum();
    })
    //Users can also directly modify the value in the form num (small bug), and calculate the subtotal in the same way$(".num").change(function() {
    let n = $(this).val();
    let price = $(this).parent().siblings(".price").html();
    price = price.substr(1);
    $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
    getSum();
   })

   function getSum() {
    let count = 0; //Calculate the total number of items let money = 0; //Calculate the total price $(".num").each(function(index) {
     if ($(".ed").eq(index).prop("checked") == true) {
      count += parseInt($(".num").eq(index).val());
      money += parseFloat($(".small_total").eq(index).text().substr(1));
     }
    })
    $(".num_sum").html(count);
    $(".sum").html(money.toFixed(2));
   }

   //Delete product module //Click delete to delete the current product, so start from $(this) $(".delete").click(function() {
     //Delete the current item$(this).parent().remove();
     $(".ed").change();
     getSum();
     clearCheckAll();
    })
    //Delete the selected product: If the small checkbox is selected, delete the corresponding product $(".delSome").click(function() {
     //Delete the selected item$(".ed:checked").parent().parent().remove();
     getSum();
     clearCheckAll();
    })
    // Clear the shopping cart $(".delAll").click(function() {
    $(".tab tbody").empty();
    getSum();
    clearCheckAll();
   })

   function clearCheckAll() {
    if ($(".tab tbody")[0].innerText == '') {
     $(".checkAll").prop("checked", false);
    }
   }
})

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Implementing a shopping cart using jQuery
  • jQuery implements the basic functions of the shopping cart
  • jQuery implements the total price calculation and total price transfer function of the shopping cart
  • jQuery implements all shopping cart functions

<<:  jQuery implements accordion effects

>>:  js method to delete a field in an object

Recommend

A detailed introduction to seata docker high availability deployment

Version 1.4.2 Official Documentation dockerhub st...

Solution to JS out-of-precision number problem

The most understandable explanation of the accura...

Understanding innerHTML

<br />Related articles: innerHTML HTML DOM i...

Correct way to load fonts in Vue.js

Table of contents Declare fonts with font-face co...

How to use custom images in Html to display checkboxes

If you need to use an image to implement the use ...

Detailed explanation of the payment function code of the Vue project

1. Alipay method: Alipay method: Click Alipay to ...

Three ways to forward linux ssh port

ssh is one of the two command line tools I use mo...

Vue.js implements the nine-grid image display module

I used Vue.js to make a nine-grid image display m...

jquery+springboot realizes file upload function

This article example shares the specific code of ...

Use Docker to build a Redis master-slave replication cluster

In a cluster with master-slave replication mode, ...

Detailed explanation of the difference between var, let and const in JavaScript

Table of contents As a global variable Variable H...

js implements a simple English-Chinese dictionary

This article shares the specific code of js to im...

Detailed explanation of data types and schema optimization in MySQL

I'm currently learning about MySQL optimizati...

mysql create database, add users, user authorization practical method

1. Create a MySQL database 1. Create database syn...