Home  >  Article  >  WeChat Applet  >  WeChat Mini Program Mall Project Shopping Quantity Addition and Subtraction

WeChat Mini Program Mall Project Shopping Quantity Addition and Subtraction

不言
不言Original
2018-06-23 16:49:013209browse

This article mainly introduces the function of adding and subtracting the shopping quantity in the WeChat mini program mall in detail. It has a certain reference value. Interested friends can refer to it

When we buy the baby, The quantity of shopping is often what we need to use, as shown below:
In the baby details page:

##In the shopping cart:


Now I will introduce this small component to you. How to write it in the small program

The picture below is the picture of this project:


wxml:


<!-- 主容器 --> 
<view class="stepper"> 
  <!-- 减号 --> 
  <text class="{{minusStatus}}" bindtap="bindMinus">-</text> 
  <!-- 数值 --> 
  <input type="number" bindchange="bindManual" value="{{num}}" /> 
  <!-- 加号 --> 
  <text class="normal" bindtap="bindPlus">+</text> 
</view>

wxss:


/*全局样式*/ 
page { 
  padding: 20px 0; 
} 
 
/*主容器*/ 
.stepper { 
  width: 80px; 
  height: 26px; 
  /*给主容器设一个边框*/ 
  border: 1px solid #ccc; 
  border-radius: 3px; 
  margin:0 auto; 
} 
 
/*加号和减号*/ 
.stepper text { 
  width: 19px; 
  line-height: 26px; 
  text-align: center; 
  float: left; 
} 
 
/*数值*/ 
.stepper input { 
  width: 40px; 
  height: 26px; 
  float: left; 
  margin: 0 auto; 
  text-align: center; 
  font-size: 12px; 
  /*给中间的input设置左右边框即可*/ 
  border-left: 1px solid #ccc; 
  border-right: 1px solid #ccc; 
} 
 
/*普通样式*/ 
.stepper .normal{ 
  color: black; 
} 
 
/*禁用样式*/ 
.stepper .disabled{ 
  color: #ccc; 
}

js:


Page({ 
  data: { 
    // input默认是1 
    num: 1, 
    // 使用data数据对象设置样式名 
    minusStatus: &#39;disabled&#39; 
  }, 
  /* 点击减号 */ 
  bindMinus: function() { 
    var num = this.data.num; 
    // 如果大于1时,才可以减 
    if (num > 1) { 
      num --; 
    } 
    // 只有大于一件的时候,才能normal状态,否则disable状态 
    var minusStatus = num <= 1 ? &#39;disabled&#39; : &#39;normal&#39;; 
    // 将数值与状态写回 
    this.setData({ 
      num: num, 
      minusStatus: minusStatus 
    }); 
  }, 
  /* 点击加号 */ 
  bindPlus: function() { 
    var num = this.data.num; 
    // 不作过多考虑自增1 
    num ++; 
    // 只有大于一件的时候,才能normal状态,否则disable状态 
    var minusStatus = num < 1 ? &#39;disabled&#39; : &#39;normal&#39;; 
    // 将数值与状态写回 
    this.setData({ 
      num: num, 
      minusStatus: minusStatus 
    }); 
  }, 
  /* 输入框事件 */ 
  bindManual: function(e) { 
    var num = e.detail.value; 
    // 将数值与状态写回 
    this.setData({ 
      num: num 
    }); 
  } 
})

Run result:


The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Tools required for WeChat mini program shopping mall system development

Train ticket query for WeChat mini program Code

The above is the detailed content of WeChat Mini Program Mall Project Shopping Quantity Addition and Subtraction. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn