本文转自:http://blog.csdn.net/michael_ouyang/article/details/70194144

我们在购买宝贝的时候,购物的数量,经常是我们需要使用的,如下所示:

在宝贝详情页里:

在购物车里:

现在就为大家介绍这个小组件,在小程序中,该如何去写

下图为本项目的图:

wxml:

  1. <!-- 主容器 -->
  2. <view class="stepper">
  3. <!-- 减号 -->
  4. <text class="{{minusStatus}}" bindtap="bindMinus">-</text>
  5. <!-- 数值 -->
  6. <input type="number" bindchange="bindManual" value="{{num}}" />
  7. <!-- 加号 -->
  8. <text class="normal" bindtap="bindPlus">+</text>
  9. </view>
<!-- 主容器 -->
<view class="stepper">
<!-- 减号 -->
<text class="{{minusStatus}}" bindtap="bindMinus">-</text>
<!-- 数值 -->
<input type="number" bindchange="bindManual" value="{{num}}" />
<!-- 加号 -->
<text class="normal" bindtap="bindPlus">+</text>
</view>

wxss:

  1. /*全局样式*/
  2. page {
  3. padding: 20px 0;
  4. }
  5. /*主容器*/
  6. .stepper {
  7. width: 80px;
  8. height: 26px;
  9. /*给主容器设一个边框*/
  10. border: 1px solid #ccc;
  11. border-radius: 3px;
  12. margin:0 auto;
  13. }
  14. /*加号和减号*/
  15. .stepper text {
  16. width: 19px;
  17. line-height: 26px;
  18. text-align: center;
  19. float: left;
  20. }
  21. /*数值*/
  22. .stepper input {
  23. width: 40px;
  24. height: 26px;
  25. float: left;
  26. margin: 0 auto;
  27. text-align: center;
  28. font-size: 12px;
  29. /*给中间的input设置左右边框即可*/
  30. border-left: 1px solid #ccc;
  31. border-right: 1px solid #ccc;
  32. }
  33. /*普通样式*/
  34. .stepper .normal{
  35. color: black;
  36. }
  37. /*禁用样式*/
  38. .stepper .disabled{
  39. color: #ccc;
  40. }
/*全局样式*/
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:

  1. Page({
  2. data: {
  3. // input默认是1
  4. num: 1,
  5. // 使用data数据对象设置样式名
  6. minusStatus: 'disabled'
  7. },
  8. /* 点击减号 */
  9. bindMinus: function() {
  10. var num = this.data.num;
  11. // 如果大于1时,才可以减
  12. if (num > 1) {
  13. num --;
  14. }
  15. // 只有大于一件的时候,才能normal状态,否则disable状态
  16. var minusStatus = num <= 1 ? 'disabled' : 'normal';
  17. // 将数值与状态写回
  18. this.setData({
  19. num: num,
  20. minusStatus: minusStatus
  21. });
  22. },
  23. /* 点击加号 */
  24. bindPlus: function() {
  25. var num = this.data.num;
  26. // 不作过多考虑自增1
  27. num ++;
  28. // 只有大于一件的时候,才能normal状态,否则disable状态
  29. var minusStatus = num < 1 ? 'disabled' : 'normal';
  30. // 将数值与状态写回
  31. this.setData({
  32. num: num,
  33. minusStatus: minusStatus
  34. });
  35. },
  36. /* 输入框事件 */
  37. bindManual: function(e) {
  38. var num = e.detail.value;
  39. // 将数值与状态写回
  40. this.setData({
  41. num: num
  42. });
  43. }
  44. })
Page({
data: {
// input默认是1
num: 1,
// 使用data数据对象设置样式名
minusStatus: 'disabled'
},
/* 点击减号 */
bindMinus: function() {
var num = this.data.num;
// 如果大于1时,才可以减
if (num > 1) {
num --;
}
// 只有大于一件的时候,才能normal状态,否则disable状态
var minusStatus = num <= 1 ? 'disabled' : 'normal';
// 将数值与状态写回
this.setData({
num: num,
minusStatus: minusStatus
});
},
/* 点击加号 */
bindPlus: function() {
var num = this.data.num;
// 不作过多考虑自增1
num ++;
// 只有大于一件的时候,才能normal状态,否则disable状态
var minusStatus = num < 1 ? 'disabled' : 'normal';
// 将数值与状态写回
this.setData({
num: num,
minusStatus: minusStatus
});
},
/* 输入框事件 */
bindManual: function(e) {
var num = e.detail.value;
// 将数值与状态写回
this.setData({
num: num
});
}
})

运行结果:

demo下载地址:http://download.csdn.net/detail/michael_ouyang/9815524

更多小程序的教程

微信开发者工具的快捷键

微信小程序的文件结构 —— 微信小程序教程系列(1)

微信小程序的生命周期实例演示 —— 微信小程序教程系列(2)

微信小程序的动态修改视图层的数据 —— 微信小程序教程系列(3)

微信小程序的新建页面 —— 微信小程序教程系列(4)

微信小程序的如何使用全局属性 —— 微信小程序教程系列(5)

微信小程序的页面跳转 —— 微信小程序教程系列(6)

微信小程序标题栏和导航栏的设置 —— 微信小程序教程系列(7)

微信小程序的作用域和模块化 —— 微信小程序教程系列(8)

微信小程序视图层的数据绑定 —— 微信小程序教程系列(9)

微信小程序视图层的条件渲染 —— 微信小程序教程系列(10)

微信小程序视图层的列表渲染 —— 微信小程序教程系列(11)

微信小程序视图层的模板 —— 微信小程序教程系列(12)

微信小程序wxss的尺寸单位rpx —— 微信小程序教程系列(13)

微信小程序的网络请求 —— 微信小程序教程系列(14)

微信小程序的百度地图获取地理位置 —— 微信小程序教程系列(15)

微信小程序使用百度api获取天气信息 —— 微信小程序教程系列(16)

微信小程序获取系统日期和时间 —— 微信小程序教程系列(17)

微信小程序之顶部导航栏实例 —— 微信小程序实战系列(1)

微信小程序之上拉加载(分页加载)实例 —— 微信小程序实战系列(2)

微信小程序之轮播图实例 —— 微信小程序实战系列(3)

微信小程序之仿android fragment之可滑动的底部导航栏实例  —— 微信小程序实战系列(4)

微信小程序之登录页实例 —— 微信小程序实战系列(5)

微信小程序之自定义toast实例 —— 微信小程序实战系列(6)

微信小程序之自定义抽屉菜单(从下拉出)实例 —— 微信小程序实战系列(7)

微信小程序之自定义模态弹窗(带动画)实例 —— 微信小程序实战系列(8)

微信小程序之侧栏分类 —— 微信小程序实战商城系列(1)

微信小程序之仿淘宝分类入口 —— 微信小程序实战商城系列(2)

更多小程序的教程请查看:http://blog.csdn.net/michael_ouyang/article/details/54700871

谢谢观看,不足之处,敬请指导

 

阅读全文      

        版权声明:本文为博主原创文章,转载务必注明出处,http://blog.csdn.net/michael_ouyang。     

[转]微信小程序之购物数量加减 —— 微信小程序实战商城系列(3)的更多相关文章

  1. [转]微信小程序之购物车 —— 微信小程序实战商城系列(5)

    本文转自:http://blog.csdn.net/michael_ouyang/article/details/70755892 续上一篇的文章:微信小程序之商品属性分类  —— 微信小程序实战商城 ...

  2. 自己动手丰衣足食之 jQuery 数量加减插件

    引言 做一个手机端的订单相关项目中,其中下订单时需要用到数量加减的控件,可以设置默认值,也可以设置最大值和最小值.使用jQuery这么长时间了,平时很少去编写属于自己的插件,现在编写的时候对立面的一些 ...

  3. web框架实现购物车数量加减

    企业开发中经常是团队协作,每个人分配一个小的模块,比如说购物车模块,数量加减这一块人们首先想到的就是通过jquery实现,其实作为一个后端接口开发的程序猿也可以用自己更擅长的后端的逻辑代码来实现,那我 ...

  4. js实现输入框数量加减【转】

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. js jquery 权限单选 bug修改以及正确代码 购物车数量加减

    效果图废话不多直接上代码 用的avalon渲染,其实都是一样的 <div class="shop-arithmetic"> <a href="javas ...

  6. js实现购买数量加减效果

    写在前面:当我们需要在多个页面都有操作数量的需求时的一种解决方案 结构: js代码: <script type="text/javascript"> function ...

  7. vue 入门 ------简单购物车功能实现(全选,数量加减,价格加减)

    简易购物车功能(无任何布局 主要是功能) 数量的加减 商品的总价钱 全选与全不选 删除(全选.价格 受影响) <script src="https://cdn.jsdelivr.net ...

  8. 【小程序开发】购物车加减几件demo

    <!-- 主容器 --> <view class="stepper"> <!-- 减号 --> <text class="{{m ...

  9. 初学Vue之数量加减

    效果图: HTML: <div class="count3"> <ul> <li v-for="(key,idx) in liList&qu ...

随机推荐

  1. SQL触发器操作

    Deleted表用于存储DELETE和UPDATE语句所影响的行的复本.在执行DELETE或UPDATE语句时,行从触发器表中删除,并传输到deleted表中.Deleted表和触发器表通常没有相同的 ...

  2. Day 2 Python 基础数据类型

    2.os.path.join()函数 语法:  os.path.join(path1[,path2[,......]]) 返回值:将多个路径组合后返回 注:第一个绝对路径之前的参数将被忽略 1 2 3 ...

  3. fedora 国内源

    wget http://mirrors.163.com/.help/fedora-163.repowget http://mirrors.163.com/.help/fedora-updates-16 ...

  4. 792. Number of Matching Subsequences

    Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...

  5. Mysql错误处理: /usr/bin/mysqld_safe: line xxx: xxxx Killed ... (mysql自动停止 Plugin FEDERATED is disabled 的完美解决方法)

    哈哈哈,问题总算解决,内心抑不住的开心 centos mysql 问题:Plugin 'FEDERATED' is disabled. /usr/sbin/mysqld: Table 'mysql.p ...

  6. springboot入门之简单demo

    项目构建 我们采用maven构建SpringBoot工程,首先创建一个maven工程,对应的pom文件如下: <properties> <java.version>1.8< ...

  7. Oracle 数据类型 与C#映射关系

    大部分类型的对应关系:原文:http://2143892.blog.51cto.com/2133892/499353 序号 Oracle数据类型 .NET类型 GetOracleValue类型 DbT ...

  8. 《JAVA与模式》之桥梁模式

    在阎宏博士的<JAVA与模式>一书中开头是这样描述桥梁(Bridge)模式的: 桥梁模式是对象的结构模式.又称为柄体(Handle and Body)模式或接口(Interface)模式. ...

  9. C#之初识单例模式

    当我们使用QQ的时候就会发现,他可以启动多个QQ,但是有时候,我们不想这样做,这时候我们就需要使用到单例模式. 1.将Form2的构造函数转为私有 using System.Windows.Forms ...

  10. paddlepaddle使用(一)

    paddlepaddle是百度提出来的深度学习的框架,个人感觉其实和tensorflow差不多(语法上面),因为本人也是初学者,也不是很懂tensorflow,所以,这些都是个人观点. 百度的padd ...