js realizes the addition and subtraction of shopping cart goods

  • 2021-08-28 07:10:00
  • OfStack

In this paper, we share the specific code of adding and subtracting the number of shopping carts by js for your reference. The specific contents are as follows

Html


<link rel='stylesheet' type='text/css' media='screen' href='../css/bootstrap.min.css'>
<script src="../js/jquery-1.12.4.js"></script>


<div style="width: 300px;margin: 30px auto 0;">
  <form class="form-inline">
   <div class="form-group">
    <div class="input-group">
     <div onclick="minus()" class="input-group-addon">-</div>
     <input type="text" class="form-control" id="exampleInputAmount">
     <div onclick="add()" class="input-group-addon">+</div>
    </div>
   </div>
  </form>
</div>

CSS


<style>
  .list a {
   display: block;
   margin: 30px;
   padding: 0 20px;
   height: 100px;
  }

  .list .list-img {
   width: 180px;
   height: 100px;
   border-radius: 6px;
   object-fit: cover;
  }

  .list .title {
   font-size: 16px;
   font-weight: bold;
   white-space: nowrap;
   text-overflow: ellipsis;
   margin: 10px 0 0;
  }

  .list .content {
   font-size: 14px;
   line-height: 26px;
   margin: 10px 0 0;
   display: -webkit-box;
   -webkit-box-orient: vertical;
   -webkit-line-clamp: 2;
   overflow: hidden;
  }
</style>

Js


<script>
 var num = 0;
  //  Initial assignment to the form 
  $('#exampleInputAmount').val(num);
  //  When you click Add 
  function add() {
   //  Method body 
   if (num >= 5) {
    alert(' Can be added at most 5 A ');
    return;
   }
   num++;
   console.log(num);
   $('#exampleInputAmount').val(num);
  }
  //  When you click Delete 
  function minus() {
   if (num < 1) {
    alert(' Has been reduced to a minimum ');
    return;
   }
   num--;
   console.log(num);
   $('#exampleInputAmount').val(num);
  }
</script>

Related articles: