Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of shopping cart with native js

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to realize the shopping cart function of native js". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

* add or subtract the number of items in the shopping cart. Note that the quantity is at least 1, and the quantity input can only be a number.

* implement the removal of shopping cart items

* you can design several more product information on the same page, each item has a price, picture, name, and add to the shopping cart button, click the button to add to the shopping cart

* to calculate the total price of shopping cart goods

Commodity html,body {margin: 0;} .row {width:100%; height:300px;} .col {width:200px; height:216px; margin: 0px 1px 0px 1px; padding-top: 2px; border:solid black 1px; float:left; text-align:center;} .col img {width: 150px } function calculate () {var tbody = document.getElementById ("tb"); var prices = tbody.querySelectorAll ("td:nth-child (4)"); var numbers = tbody.querySelectorAll ("td:nth-child (5) > input [type=number]"); var checkboxes = tbody.querySelectorAll ("th:nth-child (1) > input"); / / console.log (prices) / / console.log (numbers); / / console.log (checkboxes); var total=0; for (var I = 0; I

< prices.length; i++) { console.log("价格:" +parseInt(prices[i].innerText)+" 数量:"+parseInt(numbers[i].value) + "是否勾选:" + checkboxes[i].checked); if(checkboxes[i].checked){ total += parseInt(prices[i].innerText)*parseInt(numbers[i].value); } } console.log("总价格:" + total); document.getElementById("total").innerText = total; } // 移除商品 function del(me) { var tr = me[xss_clean][xss_clean]; var tbody = tr[xss_clean]; tbody.removeChild(tr); calculate(); } // 增加商品数量 function jia(me) { var td = me[xss_clean]; var inputs = td.getElementsByTagName("input"); // 找到此td下所有input标签 // inputs[1].value = inputs[1].value - 0 + 1; // 用-0的办法转为数字 // parseInt 将字符串转整数 parseFloat 将字符串转小数 inputs[1].value = parseInt(inputs[1].value) + 1; calculate(); } // 减少商品数量 function jian(me) { var td = me[xss_clean]; var num = td.querySelector("input[type=number]");// 只查找有type=number属性的input标签 var r = num.value - 1; if( r >

= 1) {/ / change the value of the text box num.value = r; calculate ();}} / / change the selected state of the check box function check (me) {var tbody = document.getElementById ("tb") only if the reduced result is greater than or equal to 1 Var inputs = tbody.querySelectorAll ("th input"); for (var I = 0; I commodity 1

3000.00 Commodity 2

2000.00

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report