小程序单选和多选

记录一下:小程序的单选和多选

废话不多说,上代码~

wxml


<view class="content">
  <view class="title">多选</view>
  <view class="item">
    <view class="{{item.active}}" wx:for="{{list}}" wx:key="index" wx:for-item="item" bindtap="bindList" data-id="{{item.id}}">{{item.name}}</view>
  </view>
</view>

<view class="content">
  <view class="title">单选</view>
  <view class="item">
    <view class="{{item.active?'active':''}}" wx:for="{{type}}" wx:key="index" wx:for-item="item" bindtap="bindActive" data-id="{{item.id}}">{{item.name}}</view>
  </view>
</view>

wxss

.content{
  margin-bottom: 50rpx;
}
.title{
  font-size: 40rpx;
  font-weight: bold;
}
.item > view{
  color: #666666;
  font-size: 24.8rpx;
  background: #EEEEEE;
  padding: 11rpx 27rpx;
  border-radius: 24rpx;
  margin-top: 16rpx;
  margin-right: 16rpx;
  cursor: pointer;
}
.item .active{
  color:#ffffff;
  background-color: #1D80F5;
} 

js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[{"id":"452113a3","name":"肥肥","active":''},
    	{"id":"a1b931a0","name":"滚滚","active":''},
    	{"id":"115af7f9","name":"圆圆","active":''},
    	{"id":"60fe875c","name":"胖胖","active":''},
      {"id":"8f100654","name":"嘟嘟","active":''}],
    type:[
        { id:'sgyl',name:'单选11111',active:false},     
        { id:'js',name:'单选22222',active:false},
        { id:'shjjfz',name:'单选33333',active:false},
        { id:'sbhqs',name:'单选4444',active:false},
        { id:'ldmf',name:'单选5555',active:false},
        { id:'xfgw',name:'单选66666',active:false}
    ]
  },

  //单选
  bindActive(e){
    let arr = this.data.type.map(item=>{
      if(item.id===e.currentTarget.dataset.id){
        item.active=!item.active
      }else{
        item.active=false
      }
      return item
    })    
    this.setData({type:arr});
    this.getPeopleInfo();
  },

//多选
  bindList(e){
    let index=e.currentTarget.dataset.id;
    let arr=this.data.list.map(item=>{      
      if(item.id == index && item.active == ""){
        item.active="active";
      }else if(item.id == index){
        item.active="";
      } 
      return item
    })       
    this.setData({list:arr});
    this.getPeopleInfo();
  },

//获取id
  getPeopleInfo(){
    let ids=this.data.list.filter( item=> item.active == 'active').map(item => item.id);
    let dx=this.data.type.filter(item => item.active == true).map(item=>item.id);
    console.log('多选Id:'+ids)
    console.log('单选Id:'+dx);
  }
})

示例图

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序中可以通过使用组件来实现单选多选和全选功能。 单选: 可以使用radio组件来实现单选功能。radio组件需要与radio-group组件配合使用。当用户点击其中一个radio后,radio-group会自动将其它的radio选项取消选中。 多选: 可以使用checkbox组件来实现多选功能。checkbox组件需要与checkbox-group组件配合使用。当用户点击某个checkbox时,checkbox-group会自动将其它的checkbox选项保持原有状态。 全选: 可以使用checkbox组件来实现全选功能。需要在checkbox组件中加入一个value属性,并将其绑定到全选按钮的checked属性上。当用户点击全选按钮时,将全选按钮的状态传递给所有的checkbox选项即可。 示例代码: 单选: ``` <radio-group> <label wx:for="{{list}}" wx:key="{{index}}"> <radio value="{{item.value}}">{{item.name}}</radio> </label> </radio-group> ``` 多选: ``` <checkbox-group> <label wx:for="{{list}}" wx:key="{{index}}"> <checkbox value="{{item.value}}">{{item.name}}</checkbox> </label> </checkbox-group> ``` 全选: ``` <checkbox value="all" checked="{{allChecked}}" bindchange="onAllChange">全选</checkbox> <checkbox-group> <label wx:for="{{list}}" wx:key="{{index}}"> <checkbox value="{{item.value}}" checked="{{item.checked}}">{{item.name}}</checkbox> </label> </checkbox-group> ``` 其中,allChecked和item.checked为数据中的属性,需要在js文件中定义和更新。onAllChange为全选按钮的change事件处理函数,代码如下: ``` onAllChange: function (e) { const isChecked = e.detail.value.length > 0; const list = this.data.list.map(item => { item.checked = isChecked; return item; }); this.setData({ allChecked: isChecked, list }); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值