如何在低于" 1"使用JavaScript?

时间:2018-01-15 20:17:03

标签: javascript conditional counter

需要帮助,当数字低于" 1"时,如何让我的计数器停止计数? ?我需要让计数器从#1;' 1开始。 (我已经这样做了),当用户点击减号时,当金额为1时重新启动到1))之后,我把我的金额放在购物车div中,所以我显示了我的产品数量。这是我的代码:



var plus = document.getElementById('plus');
var minus = document.getElementById('minus');
var amount = document.getElementById('amount');
var button2 = document.getElementById('add');
var cart = document.getElementById('cart'); 


  count = 1;

    plus.onclick = function() {
      count += 1;
      amount.innerHTML = count;
    };

   minus.onclick = function(){
       count-=1;
       amount.innerHTML =count;
    };
 
    button2.onclick = function(){
        cart.innerHTML = "products:" + count;
     }

body {
  margin: 0;
  padding: 0;
  font-family: 'Lato', sans-serif;
  font-size: 20pt;
  font-weight: normal;
  background: red;
  /* For browsers that do not support gradients */
  background: -webkit-linear-gradient(-90deg, red, yellow);
  /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(-90deg, red, yellow);
  /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(-90deg, red, yellow);
  /* For Firefox 3.6 to 15 */
  background: linear-gradient(-90deg, red, yellow);
  /* Standard syntax */
}

.main {
  height: 200px;
  margin: 100px auto;
  text-align: center;
}

button {
  padding: 20px 25px;
  background: transparent;
  font-size: 18pt;
  border: 1px solid gold;
  color: yellow;
  outline: 0;
}
#add{
  
}

<div class="main">

    <button id="minus">Minus</button>
    <div id="amount"> 1</div>
    <button id="plus">plus</button>
   
    
    <button id="add">add to cart</button>
    <div id="cart">products: </div>
    
  </div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

if (count === 1) return;添加到minus.onclick事件的开头:

minus.onclick = function() {
  if (count === 1) return;
  count -= 1;
  amount.innerHTML = count;
};

这样,当count为1时,函数体的其余部分将不会运行。换句话说,count -= 1;和下一行将不会运行,意味着{{1变量不会低于1。

请注意count与:

相同
if (count === 1) return;

完整的代码段:

if (count === 1) {
  return;
}
var plus = document.getElementById('plus');
var minus = document.getElementById('minus');
var amount = document.getElementById('amount');
var button2 = document.getElementById('add');
var cart = document.getElementById('cart'); 


    count = 1;

    plus.onclick = function() {
      count += 1;
      amount.innerHTML = count;
    };

    minus.onclick = function(){
      if (count === 1) return;
      count-=1;
      amount.innerHTML =count;
    };
 
    button2.onclick = function(){
      cart.innerHTML = "products:" + count;
    }
body {
  margin: 0;
  padding: 0;
  font-family: 'Lato', sans-serif;
  font-size: 20pt;
  font-weight: normal;
  background: red;
  /* For browsers that do not support gradients */
  background: -webkit-linear-gradient(-90deg, red, yellow);
  /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(-90deg, red, yellow);
  /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(-90deg, red, yellow);
  /* For Firefox 3.6 to 15 */
  background: linear-gradient(-90deg, red, yellow);
  /* Standard syntax */
}

.main {
  height: 200px;
  margin: 100px auto;
  text-align: center;
}

button {
  padding: 20px 25px;
  background: transparent;
  font-size: 18pt;
  border: 1px solid gold;
  color: yellow;
  outline: 0;
}
#add{
  
}

答案 1 :(得分:0)

只需修改<div class="main"> <button id="minus">Minus</button> <div id="amount"> 1</div> <button id="plus">plus</button> <button id="add">add to cart</button> <div id="cart">products: </div> </div>的事件点击,然后添加以下条件

minus
var plus = document.getElementById('plus');
var minus = document.getElementById('minus');
var amount = document.getElementById('amount');
var button2 = document.getElementById('add');
var cart = document.getElementById('cart'); 


  count = 1;

    plus.onclick = function() {
      count += 1;
      amount.innerHTML = count;
    };

   minus.onclick = function(){
       if(count>0){
       count-=1;
       amount.innerHTML =count;
       }
    };
 
    button2.onclick = function(){
        cart.innerHTML = "products:" + count;
     }
body {
  margin: 0;
  padding: 0;
  font-family: 'Lato', sans-serif;
  font-size: 20pt;
  font-weight: normal;
  background: red;
  /* For browsers that do not support gradients */
  background: -webkit-linear-gradient(-90deg, red, yellow);
  /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(-90deg, red, yellow);
  /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(-90deg, red, yellow);
  /* For Firefox 3.6 to 15 */
  background: linear-gradient(-90deg, red, yellow);
  /* Standard syntax */
}

.main {
  height: 200px;
  margin: 100px auto;
  text-align: center;
}

button {
  padding: 20px 25px;
  background: transparent;
  font-size: 18pt;
  border: 1px solid gold;
  color: yellow;
  outline: 0;
}
#add{
  
}

答案 2 :(得分:0)

 minus.onclick = function() {
   if (count > 1) {
     count-=1;
   }
   amount.innerHTML =count;
};

演示:https://jsfiddle.net/Lwo6347z/