小程序 之checkbox按钮选中事件

目的:
当复选框组中的第一项被选中时,显示红色线框标出的部分;若复选框组中的第一项未被选中,红色线框区域内的内容不显示
在这里插入图片描述

  1. xml部分代码如下:
<view class="weui-flex">
	<view class="weui-flex__item">
		<view class="placeholder">
			<view class="wehx-custom_title">保险</view>
			<checkbox-group bindchange="checkboxChange" class="wehx-custom_content weui-flex">
				<label class="checkbox" wx:for="{{items}}" wx:key="{{index}}" class="weui-flex__item">
					<checkbox value="{{item.name}}" wx:key="{{index}}" checked="{{item.checked}}"/>{{item.value}}
				</label>
			</checkbox-group>
		</view>
	</view>
</view>
<block  wx:if="{{aa==true}}">
	<view class="weui-flex">
		<view class="weui-flex__item" bindchange="bindDateChange">
			<view class="placeholder">
				<view class="wehx-custom_title">做箱日期</view>
				<view class="wehx-custom_content">
					<picker mode="date" value="{{useContainerDate}}">
						<view class="picker">
							{{useContainerDate}}
							<text class="iconfont iconfont-gray icon-jiantou"></text>
						</view>
					</picker>
				</view>
			</view>
		</view>
	</view>
	<view class="weui-flex">
		<view class="weui-flex__item">
			<view class="placeholder">
				<view class="wehx-custom_title">金额</view>
				<view class="wehx-custom_content">
					<input class="weui-input" value='' bindinput='outPlateNoInput' bindblur='outPlateNoInput' placeholder="请输入金额" placeholder-class="wehx-input_placeholder" />
				</view>
			</view>
		</view>
	</view>
</block>

  1. js部分代码如下:
data: {
    //使用一份保险和买两份保险
    items: [
      { name:"使用一份保险", value: '使用一份保险'},
      { name:"购买两份保险", value: '购买两份保险',},
    ],
    aa:false,//默认不使用
    //做箱日期
    useContainerDate: util.formatDate(new Date()),
  },
//复选框事件
  checkboxChange: function (e) {
    console.log('checkbox发生change事件,携带value值为:', e.detail.value)
    this.setData({
      aa:e.detail.value.indexOf('使用一份保险')!==-1?true:false
      // 找不到的时候true   找到的时候false
    })
  },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
小程序中的 checkbox 组件默认是多选框,如果要实现单选按钮的效果可以采用以下两种方式: 1. 使用 radio 组件代替 checkbox 组件 radio 组件是单选框组件,只能选择其中一个选项。可以使用 radio-group 组件来将多个 radio 组件包裹在一起,这样就能实现单选按钮的效果。 示例代码: ```html <radio-group bindchange="onChange"> <label class="radio"> <radio value="A" checked="{{true}}">选项 A</radio> </label> <label class="radio"> <radio value="B">选项 B</radio> </label> <label class="radio"> <radio value="C">选项 C</radio> </label> </radio-group> ``` 2. 在 checkbox 组件上添加点击事件,手动实现单选按钮的效果 示例代码: ```html <checkbox-group bindchange="onChange"> <label class="checkbox"> <checkbox value="A" checked="{{true}}" data-name="A" data-checked="{{true}}" bindtap="onTap">选项 A</checkbox> </label> <label class="checkbox"> <checkbox value="B" data-name="B" data-checked="{{false}}" bindtap="onTap">选项 B</checkbox> </label> <label class="checkbox"> <checkbox value="C" data-name="C" data-checked="{{false}}" bindtap="onTap">选项 C</checkbox> </label> </checkbox-group> ``` JS代码: ```javascript Page({ data: { items: [ {name: 'A', checked: true}, {name: 'B', checked: false}, {name: 'C', checked: false}, ], }, onTap: function(event) { var name = event.currentTarget.dataset.name; var checked = event.currentTarget.dataset.checked; var items = this.data.items; for (var i = 0; i < items.length; i++) { if (items[i].name == name) { items[i].checked = !checked; } else { items[i].checked = false; } } this.setData({ items: items, }); }, onChange: function(event) { console.log(event.detail.value); }, }); ``` 在 onTap 函数中,首先获取当前点击的选项的名称和状态(是否选中),然后遍历所有选项,将当前选项设置为选中状态,其它选项设置为未选中状态。最后调用 setData 函数更新数据即可。注意要将 checkbox 的 checked 属性设置为 false,否则会出现多选的情况。 以上两种方式都可以实现单选按钮的效果,具体选择哪一种方式取决于具体需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值