jQuery implements shopping cart function

jQuery implements shopping cart function

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

html

<!DOCTYPE html>
<html>
<head>
<title>Shopping Cart</title>
<meta charset="utf-8" />
<style type="text/css">
h1 {
    text-align: center;
}
 
table {
    margin: 0 auto;
    width: 60%;
    border: 2px solid #aaa;
    border-collapse: collapse;
}
 
table th, table td {
    border: 2px solid #aaa;
    padding: 5px;
}
 
th {
    background-color: #eee;
}
</style>
<script src="jquery-3.2.1.min.js"></script>
<script src="gw.js"></script>
</head>
<body>
    <h1>What a bargain</h1>
    <table id="tb1">
        <tr>
            <th>Products</th>
            <th>Unit price (yuan)</th>
            <th>Color</th>
            <th>Inventory</th>
            <th>Positive Rate</th>
            <th>Operation</th>
        </tr>
        <tr>
            <td>Logitech M185 Mouse</td>
            <td>80</td>
            <td>Black</td>
            <td>893</td>
            <td>98%</td>
            <td align="center"><input type="button" value="Add to cart"
                onclick="addshoping(this);" /></td>
        </tr>
        <tr>
            <td>Microsoft X470 Keyboard</td>
            <td>150</td>
            <td>Black</td>
            <td>9028</td>
            <td>96%</td>
            <td align="center"><input type="button" value="Add to cart"
                onclick="addshoping(this);" /></td>
        </tr>
        <tr>
            <td>Locke iPhone 6 case</td>
            <td>60</td>
            <td>Transparent</td>
            <td>672</td>
            <td>99%</td>
            <td align="center"><input type="button" value="Add to cart"
                onclick="addshoping(this);" /></td>
        </tr>
        <tr>
            <td>Bluetooth headset</td>
            <td>100</td>
            <td>Blue</td>
            <td>8937</td>
            <td>95%</td>
            <td align="center"><input type="button" value="Add to cart"
                onclick="addshoping(this);" /></td>
        </tr>
        <tr>
            <td>Kingston USB flash drive</td>
            <td>70</td>
            <td>Red</td>
            <td>482</td>
            <td>100%</td>
            <td align="center"><input type="button" value="Add to cart"
                onclick="addshoping(this);" /></td>
        </tr>
    </table>
 
    <h1>Shopping Cart</h1>
    <table>
        <thead>
            <tr>
                <th>Products</th>
                <th>Unit price (yuan)</th>
                <th>Quantity</th>
                <th>Amount (Yuan)</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody id="goods">
 
        </tbody>
        <tfoot>
            <tr>
                <td colspan="3" align="right">Total</td>
                <td id="total"></td>
                <td></td>
            </tr>
        </tfoot>
    </table>
 
</body>
</html>

js

//Add shopping cart function addshoping(btn) {
 
        var name = $(btn).parent().siblings().eq(0).html()
        var price = $(btn).parent().siblings().eq(1).html()
 
         var trs = $("#goods tr")
         var nameArr = new Array();
         $.each(trs, function (index, value) {
             nameArr.push($(this).children('td').eq(0).text())
         })
        var $tr = $('<tr>' +
            '<td>' + name + '</td>' +
            '<td>' + price + '</td>' +
            '<td align="center">' +
            '<input type="button" value="-" onclick="jian(this);"/> ' +
            '<input type="number" size="3" readonly value="1"/> ' +
            '<input type="button" value="+" onclick="increase(this);"/>' +
            '</td>' +
            '<td>' + price + '</td>' +
            '<td align="center"><input type="button" value="x" onclick="del(this);"/></td>' +
            '</tr>');
        var ishasName = nameArr.indexOf(name)
        
        if (ishasName >= 0) {
            var goodcount = trs.eq(ishasName).children('td').eq(2).children().eq(1).val()
            Number.parseInt(goodcount);
            trs.eq(ishasName).children('td').eq(2).children().eq(1).val(++goodcount)
            var price=trs.eq(ishasName).children('td').eq(1).html()
             Number.parseInt(price)
             trs.eq(ishasName).children('td').eq(3).html(goodcount*price);
            
                
        } else {
 
           // $tr.insertAfter($("#goods tr:eq(0)"))
            $("#goods").append($tr);
         }
            var kucun = tds.eq(3).html()
   Number.parseInt(kucun)
   tds.eq(3).html(--kucun)
 
        sum()
    }
    //Increase function increase(btn){
        var num=$(btn).prev().val()
        Number.parseInt(num);
        var bignew=$(btn).prev().val(++num)
        var price = $(btn).parent().prev().html();
        $(btn).parent().next().html(num*price);
        sum();
    
    };
    //Reduce function jian(btn){
        
        var num = $(btn).next().val()
        if(num<=1){
            return;
        }
        Number.parseInt(--num)
        var price = $(btn).parent().prev().html();
        var newprice=$(btn).parent().next().html();
        
        $(btn).parent().next().html(newprice-price);
        $(btn).next().val(num)
      
    sum()
    }
    //delete function del(btn){
       $(btn).parent().parent().remove()
       sum();
    }
    //sum function sum() {
// Get all rows under tbody var $trs = $("#goods tr");
// loop through them var sum = 0;
for (var i = 0; i < $trs.length; i++) {
    // Get each row var $tr = $trs.eq(i);
    // Get the value of the fourth column in the row (amount)
    var mny = $tr.children().eq(3).html();
    sum += parseFloat(mny);
}
// Write to total$("#total").html(sum);
};

Running results:

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:
  • jQuery implements the addition and subtraction of multiple items in the shopping cart + total price calculation
  • jQuery implements the flying animation effect of adding to the shopping cart
  • Shopping cart function implemented by JQuery (can reduce or add items and automatically calculate prices)
  • How to use jQuery to calculate the price of the shopping cart
  • Pure jQuery to imitate Taobao shopping cart settlement
  • A shopping cart similar to a shopping mall based on JQuery
  • jQuery realizes the effect of adding and subtracting numbers in the shopping cart
  • jQuery+HTML5 add shopping cart code sharing
  • How to implement the settlement function of jquery shopping cart
  • jQuery implements a Taobao shopping cart-like full selection state example

<<:  Implementation of FIFO in Linux process communication

>>:  SQL serial number acquisition code example

Recommend

MySQL 5.7.16 ZIP package installation and configuration tutorial

This article shares the installation and configur...

js to realize login and registration functions

This article example shares the specific code of ...

Examples of 4 methods for inserting large amounts of data in MySQL

Preface This article mainly introduces 4 methods ...

Solution to the failure of entering the container due to full docker space

Since the problem occurred rather suddenly and th...

Solve the problem of MySql client exiting in seconds (my.ini not found)

Problem description (environment: windows7, MySql...

Summary of MySQL's commonly used concatenation statements

Preface: In MySQL, the CONCAT() function is used ...

Tutorial on installing mysql5.7.17 via yum on redhat7

The RHEL/CentOS series of Linux operating systems...

An enhanced screenshot and sharing tool for Linux: ScreenCloud

ScreenCloud is a great little app you didn’t even...

Summary of MySQL InnoDB architecture

Table of contents introduction 1. Overall archite...

Methods and steps to upgrade MySql5.x to MySql8.x

Several Differences Between MySQL 5.x and MySQL 8...

Remote development with VSCode and SSH

0. Why do we need remote development? When develo...

Use Visual Studio Code to connect to the MySql database and query

Visual Studio Code is a powerful text editor prod...

How to use VirtualBox to simulate a Linux cluster

1. Set up HOST on the host Macbook The previous d...