Home Web Front-end JS Tutorial jQuery implements adding products to shopping cart and checking out

jQuery implements adding products to shopping cart and checking out

Apr 19, 2018 pm 04:20 PM
jquery merchandise shopping cart

This time I will bring you jQuery to implement adding products to the shopping cart and checkout, jQuery to implement adding products to the shopping cart and settling the bill. What are the precautions, the following is the actual combat Let’s take a look at the case.

<!DOCTYPE html>
<html>
  <head>
    <metacharset="UTF-8">
    <title></title>
    <scripttype="text/javascript"src="js/jquery-1.8.3.js"></script>
    <script>
    /*删除*/
      $(function(){
  $(".blue").bind("click",function(){
    $(this).parent().parent().remove();
    totalPrice();
  });
 
  /*当鼠标离开文本框时,获取当前值,调用totalPrice()函数进行结算*/
  $(".shopping_product_list_5 input").bind("blur",function(){
    var t = $(this).val();
    totalPrice();
  });
  var allPrice = 0;
  var allReduce = 0;
  var allCount = 0;
 
  $("#myTableProduct tr").each(function(){  /*循环购物车列表的每一行*/
    var num = parseInt($(this).find(".shopping_product_list_5 input").val());  /*获取文本框中数量值*/
    var price = parseFloat($(this).find(".shopping_product_list_4 label").text()); /* 获取商品价格*/
    var total = price * num;
    allPrice += total; /*计算所有商品的总价格*/
 
    /*获取节省的金额*/
    var reduce = parseFloat($(this).find(".shopping_product_list_3 label").text()) - parseFloat($(this).find(".shopping_product_list_4 label").text());
    var reducePrice = reduce*num;
    allReduce +=reducePrice;
 
    /*获取积分*/
    var count = parseFloat($(this).find(".shopping_product_list_2 label").text());
    allCount +=count;
  });
  $("#product_total").text(allPrice.toFixed(2)); /*填写计算结果,其中利用toFixed()函数保留两位小数*/
  $("#product_save").text(allReduce.toFixed(2));
  $("#product_integral").text(allCount.toFixed(2));
});
 function totalPrice(){
  var allPrice = 0;
  var allReduce = 0;
  var allCount = 0;
  $("#myTableProduct tr").each(function(){
    var num = parseInt($(this).find(".shopping_product_list_5 input").val());
    var price = parseFloat($(this).find(".shopping_product_list_4 label").text());
    var total = price * num;
    allPrice += total;
 
    var reduce = parseFloat($(this).find(".shopping_product_list_3 label").text()) - parseFloat($(this).find(".shopping_product_list_4 label").text());
    var reducePrice = reduce*num;
    allReduce +=reducePrice;
 
    var count = parseFloat($(this).find(".shopping_product_list_2 label").text());
    allCount +=count;
  });
  $("#product_total").text(allPrice.toFixed(2));
  $("#product_save").text(allReduce.toFixed(2));
  $("#product_integral").text(allCount.toFixed(2));
 }
    </script>
  </head>
  <body>
    <pclass="shopping_list_top">您已选购以下商品</p>
  <pclass="shopping_list_border">
    <tablewidth="100%"border="1px solid #ccc">
      <trclass="shopping_list_title">
        <tdclass="shopping_list_title_1">商品名</td>
        <tdclass="shopping_list_title_2">单品积分</td>
        <tdclass="shopping_list_title_3">市场价</td>
        <tdclass="shopping_list_title_4">当当价</td>
        <tdclass="shopping_list_title_5">数量</td>
        <tdclass="shopping_list_title_6">删除</td>
      </tr>
    </table>
    <tablewidth="100%"border="1px solid #ccc"id="myTableProduct">
 
      <trclass="shopping_product_list"id="shoppingProduct_01">
        <tdclass="shopping_product_list_1"><ahref="#"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">私募(首部披露资本博弈秘密的金融...</a></td>
        <tdclass="shopping_product_list_2"><label>189</label></td>
        <tdclass="shopping_product_list_3">¥<label>32.00</label></td>
        <tdclass="shopping_product_list_4">¥<label>18.90 </label>(59折)</td>
        <tdclass="shopping_product_list_5"><inputtype="text"value="1"/></td>
        <tdclass="shopping_product_list_6"><ahref="javascript:void(0)"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">删除</a></td>
      </tr>
      <trclass="shopping_product_list"id="shoppingProduct_02">
        <tdclass="shopping_product_list_1"><ahref="#"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue"> 小团圆(张爱玲最神秘小说遗稿)</a></td>
        <tdclass="shopping_product_list_2"><label>173</label></td>
        <tdclass="shopping_product_list_3">¥<label>28.00</label></td>
        <tdclass="shopping_product_list_4">¥<label>17.30</label>(62折)</td>
        <tdclass="shopping_product_list_5"><inputtype="text"value="1"/></td>
        <tdclass="shopping_product_list_6"><ahref="javascript:void(0)"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">删除</a></td>
      </tr>
      <trclass="shopping_product_list"id="shoppingProduct_03">
        <tdclass="shopping_product_list_1"><ahref="#"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">不抱怨的世界(畅销全球80国的世界...</a></td>
        <tdclass="shopping_product_list_2"><label>154</label></td>
        <tdclass="shopping_product_list_3">¥<label>24.80</label></td>
        <tdclass="shopping_product_list_4">¥<label>15.40</label> (62折)</td>
        <tdclass="shopping_product_list_5"><inputtype="text"value="2"/></td>
        <tdclass="shopping_product_list_6"><ahref="javascript:void(0)"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">删除</a></td>
      </tr>
      <trclass="shopping_product_list"id="shoppingProduct_04">
        <tdclass="shopping_product_list_1"><ahref="#"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">福玛特双桶洗衣机XPB20-07S</a></td>
        <tdclass="shopping_product_list_2"><label>358</label></td>
        <tdclass="shopping_product_list_3">¥<label>458.00</label></td>
        <tdclass="shopping_product_list_4">¥<label>358.00</label> (78折)</td>
        <tdclass="shopping_product_list_5"><inputtype="text"value="1"/></td>
        <tdclass="shopping_product_list_6"><ahref="javascript:void(0)"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">删除</a></td>
      </tr>
      <trclass="shopping_product_list"id="shoppingProduct_05">
        <tdclass="shopping_product_list_1"><ahref="#"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">PHP和MySQL Web开发 (原书第4版)</a></td>
        <tdclass="shopping_product_list_2"><label>712</label></td>
        <tdclass="shopping_product_list_3">¥<label>95.00</label></td>
        <tdclass="shopping_product_list_4">¥<label>71.20</label> (75折)</td>
        <tdclass="shopping_product_list_5"><inputtype="text"value="1"/></td>
        <tdclass="shopping_product_list_6"><ahref="javascript:void(0)"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">删除</a></td>
      </tr>
      <trclass="shopping_product_list"id="shoppingProduct_06">
        <tdclass="shopping_product_list_1"><ahref="#"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">法布尔昆虫记</a>(再买¥68.30即可参加“满199元减10元现金”活动)</td>
        <tdclass="shopping_product_list_2"><label>10</label></td>
        <tdclass="shopping_product_list_3">¥<label>198.00</label></td>
        <tdclass="shopping_product_list_4">¥<label>130.70</label> (66折)</td>
        <tdclass="shopping_product_list_5"><inputtype="text"value="1"/></td>
        <tdclass="shopping_product_list_6"><ahref="javascript:void(0)"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"rel="external nofollow"class="blue">删除</a></td>
      </tr>
    </table>
    <pclass="shopping_list_end">
      <p><aid="removeAllProduct"href="javascript:void(0);"rel="external nofollow">清空购物车</a></p>
      <ul>
        <liclass="shopping_list_end_1"><inputname=""type="image"src="images/shopping_balance.gif"/></li>
        <liclass="shopping_list_end_2">¥<labelid="product_total"></label></li>
        <liclass="shopping_list_end_3">商品金额总计:</li>
        <liclass="shopping_list_end_4">您共节省金额:¥<labelclass="shopping_list_end_yellow"id="product_save"></label><br/>
          可获商品积分:<labelclass="shopping_list_end_yellow"id="product_integral"></label>
        </li>
      </ul>
    </p>
  </p>
  </body>
</html>
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the use of jquery plug-in extension

How jQuery determines the browser version

jQuery table top bar fixed effect

The above is the detailed content of jQuery implements adding products to shopping cart and checking out. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Practical tutorial: Detailed explanation of shopping cart function with PHP and MySQL Practical tutorial: Detailed explanation of shopping cart function with PHP and MySQL Mar 15, 2024 pm 12:27 PM

Practical tutorial: Detailed explanation of the shopping cart function with PHP and MySQL. The shopping cart function is one of the common functions in website development. Through the shopping cart, users can easily add the goods they want to buy to the shopping cart, and then proceed with settlement and payment. In this article, we will detail how to implement a simple shopping cart function using PHP and MySQL and provide specific code examples. To create a database and data table, you first need to create a data table in the MySQL database to store product information. The following is a simple data table

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

How to identify the authenticity of Dewu products? Methods to identify authenticity How to identify the authenticity of Dewu products? Methods to identify authenticity Mar 12, 2024 pm 12:16 PM

When we use this platform to make various choices, some of the products we buy, especially the shoes we buy, are bought with our own money. We are not sure whether they are fakes, so in order to make ourselves To feel more at ease, you can only conduct some appraisals of some products so that you can know whether they are genuine or fake. This makes you feel more at ease when buying. It also means that you can spend money more securely, at least you don’t have to wear fakes. No, so today in order to let more users know how to identify products, today the editor will explain some of the above content information to everyone, so friends who have ideas must not If you missed it, hurry up and take a look with the editor. Distinguish authenticity

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to add Meituan e-commerce products to the shopping cart_The specific steps for adding Meituan e-commerce products to the shopping cart How to add Meituan e-commerce products to the shopping cart_The specific steps for adding Meituan e-commerce products to the shopping cart Mar 27, 2024 pm 09:50 PM

1. First open the [Meituan] APP software, on the [E-commerce] page, click on a favorite [Product] option to enter the [Product Details] page. 2. Then on the [Product Details] page, click the [Add to Cart] button in the bottom function bar. 3. Then in the option list, click the [Specifications] option you like, and click the [Confirm] button to successfully add it to the shopping cart.

See all articles