Ajax+json realizes shopping cart settlement
This time I will bring you Ajax+json to realize shopping cart settlement. What are the precautions for Ajax+json to realize shopping cart settlement? The following is a practical case, let’s take a look.
<p class="goodsList_menu"> <p class="goodsList"> <p class="goodsListfl"> <input class="fl" style="margin:15px 10px 0 10px;" type="checkbox" name="" /> <ul> <li>全选</li> <li>商品</li> </ul> </p> <p class="goodsListfr fr"> <ul> <li>单价</li> <li class="w100">数量</li> <li class="w100">小计</li> <li>操作</li> </ul> </p> </p> <!--内容--> <p class='goodsListbox'></p> <p class="checkout"> <p class="checkoutleft"> <input class="fl" style="margin:21px 10px 0 10px;" type="checkbox" name="" /> <ul> <li>全选</li> <li>删除选中产品</li> </ul> </p> <p class="checkoutright fr"> <span>总价:<b>¥</b></span><span class="checkoutSum">0</span> <input type="button" value="去结算" /> </p> </p> </p>
body,html,ul,li,a{ margin:0;padding:0;font-family:"microsoft yahei";list-style:none;text-decoration:none; } .fl{ float:left; } .fr{ float:right; } .f12{ font-size:12px; } .disl{ display:inline-block; } .w100{ width:100px; } .fw{ font-weight:bold; } .goodsList_menu{ width:990px;height:45px;background:#f3f3f3;margin:0 auto;line-height:45px; font-size:14px;color:#333;border:1px solid #ddd; } .goodsList_menu .goodsListfl ul li{ float:left;margin-right:80px;cursor:pointer; } .goodsList_menu .goodsListfr ul li{ float:left;margin-right:37px;text-align:center;cursor:pointer; } /*内容*/ .goodsList_content{ width:100%;height:80px;border-bottom:1px solid #eee;padding:20px 0;margin-top:40px; } .goodsList_content .disl{ line-height:20px;width:300px;cursor:pointer;margin-left:10px; } .goodsList_content .disl span:hover{ color:#e4393c; } .goodsList_content .disl p:hover{ color:#e4393c; } .goodsListnum .listNum{ widows:45px;height:21px;width:50px;border:1px solid #eee;text-align:center; outline:none; } .goodsListnum ul li{ float:left;margin-right:30px;text-align:center;cursor:pointer; } .goodsListnum ul li a{ border:1px solid #ddd;padding:2px 7px;color:#000; } .goodsListnum ul li p{ line-height:0;color:#666;font-size:12px;margin-top:-2px; } .buy_goods p{ background:url(../images/arrow.png)no-repeat;padding-left:30px;background-position:-20px -20px; } .buy_goods{ position:relative; } /*底部结账*/ .checkout{ height:55px;border:1px solid #eee;margin-top:20px;line-height:55px; } .checkout .checkoutleft ul li{ float:left;margin-right:10px;cursor:pointer; } .checkout .checkoutSum{ font-weight:bold;font-size:18px;color:#e64346; } .checkout .checkoutright input{ border:none;color:#fff;outline:none;width:100px;height:55px;line-height:55px; font-size:20px;background:#e64346;margin-left:50px;cursor:pointer; }
<script> $.ajax({ type:"get", url:"jd.json", dataType:"json", success:function(data){ var list = data.list;//拿到list数组 //console.log(list); for(var i=0;i<list.length;i++){ var numArray = list[i]; numArray.price = (numArray.price).toFixed(2); var text = "<p class='goodsList_content'><p class='fl buy_goods'>"+ "<input class='fl' style='margin:15px 10px 0 10px;' type='checkbox' name='' />"+ "<img align='top' src='images/1.jpg' /><p class='disl'>"+ "<span>"+numArray.description+"</span><p class='f12'>购买礼品服务</p></p>"+ "<p class='disl' style='width:auto;position:absolute;right:-100px;top:0;'>"+ "<span>"+numArray.color+"</span></p></p>"+ "<p class='fr goodsListnum' style='margin-top:-12px;'>"+ "<ul><li><b>¥</b><span class='fw'>"+numArray.price+"</span></li>"+ "<li class='w100'><a class='minus' onClick='minus(this);' href='javascript:void(0);'>-</a>"+ "<input value='1' class='listNum' /><a class='add' onClick='add(this)' href='javascript:void(0);'>+</a><p>有货</p></li>"+ "<li class='w100'><b>¥</b><span id='sub' class='fw'>"+numArray.price*numArray.quentity+"</span></li>"+ "<li><a style='border:none;color:#666;' href='javascript:void(0);'>删除</a></li>"+ "</ul></p></p>" $(text).appendTo(".goodsListbox"); } }, error:function(){ console.log("调用数据失败!"); } }); var listNum,price,sums,sub1,sub2; function add(obj){ listNum = $(obj).prev().val();//input值 listNum = parseInt(listNum); var num = parseInt(listNum+1);//input值每次+1 $(obj).prev().val(num); price = $(obj).parent().prev().children('span').text();//找到单价 price = parseInt(price);//转换成number类型 price = price.toFixed(2); sums = $(obj).parent().next().children('span').text()//找到总金额 sums = parseInt(sums); console.log(typeof sums); $(obj).parent().next().children('span').text(price*num); sub1 = $(".goodsList_content:eq(0)").find("#sub").text(); sub2 = $(".goodsList_content:eq(1)").find("#sub").text(); sub1 = parseInt(sub1); sub2 = parseInt(sub2); var res = $(".checkoutright .checkoutSum").text((sub1+sub2).toFixed(2)); } function minus(obj){ listNum = $(obj).next().val(); listNum = parseInt(listNum); if(listNum<=1){ listNum==1; }else{ --listNum; } $(obj).next().val(listNum); price = $(obj).parent().prev().children('span').text();//找到单价 price = parseInt(price);//转换成number类型 price = price.toFixed(2); sums = $(obj).parent().next().children('span').text()//找到总金额 sums = parseInt(sums); $(obj).parent().next().children('span').text(price*listNum); $(".checkoutright .checkoutSum").text(price*listNum); sub1 = $(".goodsList_content:eq(0)").find("#sub").text(); sub2 = $(".goodsList_content:eq(1)").find("#sub").text(); sub1 = parseInt(sub1); sub2 = parseInt(sub2); $(".checkoutright .checkoutSum").text(sub1+sub2); } </script>
How to communicate data between C and View
How to solve the status=parsererror error reported during Ajax interaction
The above is the detailed content of Ajax+json realizes shopping cart settlement. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

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

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
