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

Solution to the problem that the InnoDB engine is disabled when MySQL is started

Find the problem Today at work, when copying tabl...

MySQL graphical management tool Navicat installation steps

Table of contents Preface 1. Arrange the installa...

Tips for viewing History records and adding timestamps in Linux

Tips for viewing History records and adding times...

How to read the regional information of IP using Nginx and GeoIP module

Install GeoIP on Linux yum install nginx-module-g...

How to redraw Button as a circle in XAML

When using XAML layout, sometimes in order to make...

Summary of 6 Linux log viewing methods

As a backend programmer, you deal with Linux in m...

Sample code for realizing book page turning effect using css3

Key Takeaways: 1. Mastering CSS3 3D animation 2. ...

Detailed explanation of MySQL sql99 syntax inner join and non-equivalent join

#Case: Query employee salary levels SELECT salary...

How to upload projects to Code Cloud in Linux system

Create a new project test1 on Code Cloud Enter th...

DOCTYPE Document Type Declaration (Must-Read for Web Page Lovers)

DOCTYPE DECLARATION At the top of every page you w...

How to split data in MySQL table and database

Table of contents 1. Vertical (longitudinal) slic...

Vue.js manages the encapsulation of background table components

Table of contents Problem Analysis Why encapsulat...