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:
|
<<: Implementation of FIFO in Linux process communication
>>: SQL serial number acquisition code example
Table of contents 1. Understanding the Equality R...
Before talking about OO, design patterns, and the ...
The effect diagram is as follows: <!DOCTYPE ht...
Perhaps when I name this article like this, someon...
Table of contents 1: Encapsulation idea 2. Packag...
Preface Components are something we use very ofte...
Encryption and decryption are an important means ...
Table of contents Preface Basic Concepts of Argum...
Preface We all know that startups initially use m...
Counting the size of each table in each database ...
1. Change password 1. Modify the password of ordi...
Scenario You need to authorize the tester to use ...
Table of contents 1. Direct assignment 2. Shallow...
Today I will take you through the history of ext4...
1. Why is eject not recommended? 1. What changes ...