위챗 애플릿 | 전환 단추나view 선택 상태

3887 단어
현재 데이터의 선택 값을 간단하게 판단하고 단추를 눌렀을 때 데이터 값을 업데이트하여 페이지를 다시 렌더링합니다.
index.wxml 파일
<view class="switch-type">
    <view class="btn {{currentSelectTripType == 'pinche' ? 'active' : ''}}" bindtap='selectedPinche' data-id='pinche'>  view>
    <view class="btn {{currentSelectTripType == 'baoche' ? 'active' : ''}}" bindtap='selectedBaoche' data-id='baoche'>  view>
  view>

index.js 파일
Page({
  data: {~~~~
    currentSelectTripType: 'pinche',
  },
  //   data       
  selectedPinche: function (e) {
    this.setData({
      currentSelectTripType: e.currentTarget.dataset.id
    })
  },
  selectedBaoche: function(e) {
    this.setData({
      currentSelectTripType: e.currentTarget.dataset.id
    })
  },
})

간단한 양식을 덧붙이다
index.wxss
.switch-type {
  display: flex;
}
.switch-type .btn:first-child {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}
.switch-type .btn:last-child {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}
.switch-type .btn {
  background: #dfdfdf;
  padding: 10rpx 40rpx;
}
.switch-type .btn.active {
  border: 2rpx solid #007bff;
  background: #fff;
}

www.shiqidu.com/d/469

좋은 웹페이지 즐겨찾기