微信小程序自定义多选

在微信小程序里面不使用checkbox怎么实现view的多选

wxml文件

<block wx:for="{{itemList}}" wx:key="id">
  <view class='item_container' bindtap='itemSelected' data-index='{{index}}'>
     <image class='select_icon' src="{{item.isSelected?'/image/selected.png':'/image/unselected.png'}}">image>
     <text class='text'>{{item.name}}text>
  view>
block>

js文件

data: {
    itemList: [
      { id: 1, name: 'C语言', isSelected: false, },
      { id: 2, name: 'Java', isSelected: false, },
      { id: 3, name: 'C++', isSelected: false, },
    ]
  },
 
itemSelected: function (e) {
    var index = e.currentTarget.dataset.index;
    var item = this.data.itemList[index];
    item.isSelected = !item.isSelected;
    this.setData({
      itemList: this.data.itemList,
    });
  }

你可能感兴趣的:(小程序,小程序)