JS implements simple addition and subtraction of shopping cart effects

JS implements simple addition and subtraction of shopping cart effects

This article example shares the specific code of JS to achieve simple addition and subtraction of shopping carts for your reference. The specific content is as follows

1. Write the input box as the effect of addition and subtraction.

2. The plus and minus buttons are implemented using button buttons

3. Use js to control the value of i as the value in the input box.

The effect achieved:

The complete code is as follows:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title></title>
  <style>
   body{
    margin: 0;
    padding: 0;
   }
   .box{
    border: red solid 1px;
    height: 40px;
    width: 200px;
    margin: 200px auto;
    text-align: center;
    padding-top: 20px;
   }
   .box input{
    width: 40px;
    text-align: center;
   }
   #money{
    border: none;
    text-align: left;
    margin-left: 2px;
   }
   ul{
    list-style: none;
   }
   ul li{
    display: block;
    float: left;
    vertical-align: middle;
   }
  </style>
  <script>
   window.onload = function(){
    var plus = document.getElementById("plus");
    var i = document.getElementById("text").value;
    var subtract = document.getElementById("subtract");
    var money = document.getElementById("money").value;
    plus.onclick = function(){
     i++;
     document.getElementById("text").value = i;
     document.getElementById("money").value = i*money;
    }
    subtract.onclick = function(){
     if (i>0) {
      i--;
      document.getElementById("text").value = i;
      document.getElementById("money").value = i*money;
     } else{
      i=0;
      document.getElementById("text").value = i;
      document.getElementById("money").value = i*money;
     }
    }    
   }  
  </script>
 </head>
 <body>
  <ul class="box">
   <li><button id="plus">+</button></li>
   <li><input type="text" id="text" value="1"/></li>
   <li><button id="subtract">-</button></li>
   <li>&nbsp;&nbsp;&nbsp;¥<input type="text" id="money" value="88"/></li>
  </ul>
 </body>
</html>

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:
  • js implements shopping cart addition and subtraction and price calculation
  • js realizes shopping cart addition and subtraction and price calculation functions
  • js to add and subtract the number of items in the shopping cart
  • How to add product components to vue.js shopping cart
  • Implementing the Add to Cart Effect Based on JavaScript with Source Code Download

<<:  MySql Installer 8.0.18 Visual Installation Tutorial with Pictures and Text

>>:  Various problems and solutions in the process of deploying Tomcat to release projects on Linux

Recommend

Vue's vue.$set() method source code case detailed explanation

In the process of using Vue to develop projects, ...

mysql5.6.8 source code installation process

Kernel: [root@opop ~]# cat /etc/centos-release Ce...

Logrotate implements Catalina.out log rotation every two hours

1. Introduction to Logrotate tool Logrotate is a ...

Detailed explanation of the use of the clip-path property in CSS

Use of clip-path polygon The value is composed of...

In-depth explanation of MySQL user account management and permission management

Preface The MySQL permission table is loaded into...

Tutorial on installing mysql5.7.36 database in Linux environment

Download address: https://dev.mysql.com/downloads...

Detailed steps to delete environment variables in Linux

How to delete environment variables in Linux? Use...

3 ways to add links to HTML select tags

The first one : Copy code The code is as follows: ...

A detailed introduction to wget command in Linux

Table of contents First install wget View Help Ma...

MySQL database backup and recovery implementation code

Database backup #grammar: # mysqldump -h server-u...

jQuery realizes dynamic particle effect

This article shares the specific code of jQuery t...

In-depth explanation of InnoDB locks in MySQL technology

Table of contents Preface 1. What is a lock? 2. L...

Paragraph layout and line breaks in HTML web pages

The appearance of a web page depends largely on i...