微信小程序 选择年和月以及回显 使用picker-view组件

html 复制代码
<!--选择年月-->
  <view bindtap="pickCalendar">{{year}}年{{month}}月</view>
  <picker-view wx:if="{{open}}" class="fixed-select" indicator-style="height: 50px;" style="width: 100%; height: 300px;" value="{{[year, month-1]}}" bindchange="bindChange">
    <view class="select-head">
      <view class="close" bindtap="closeSelect">取消</view>
      <view class="close sure" bindtap="sureSelect">确定</view>
    </view>
    <picker-view-column>
      <view wx:for="{{years}}" wx:key="this" class="picker-list">{{item}}年</view>
    </picker-view-column>
    <picker-view-column>
      <view wx:for="{{months}}" wx:key="this" class="picker-list">{{item}}月</view>
    </picker-view-column>    
  </picker-view>
javascript 复制代码
/*选择年月-start*/
const date = new Date()
const years = []
const months = []
for (let i = 1900; i <= date.getFullYear(); i++) {
  years.push(i)
}
for (let i = 1; i <= 12; i++) {
  months.push(i)
}
/*选择年月-end*/
Page({
  data: {
    /*选择日期-start*/
    years,
    months,
    year: date.getFullYear(), //年份
    month: date.getMonth() + 1, //几月
    value: [9999, 0, 0], // 获取年月份
    open: false,
    checkedYear: '',//选中的年份 最后赋值用
    checkedMonth: '',//选中的月份 最后赋值用
    /*选择日期-end*/
  },

  /*其它代码*/
  /*月份选择器-start*/  
  pickCalendar(){
    console.log(this.data.month)
    this.setData({
      open: true
    })
  },
  
  //确定选择
  sureSelect(){
    this.setData({
      // val 上面定义了 接收的是e里面的值 为数组 
      year: this.data.checkedYear || this.data.year,
      month: this.data.checkedMonth || this.data.month,
      open: false
    }) 
  },

  // 取消选择 
  closeSelect() {
    this.setData({
      open: false
    })
  },

  // 日期改变
  bindChange(e) {
    const val = e.detail.value
    this.setData({
      // val 上面定义了 接收的是e里面的值 为数组 
      checkedYear: this.data.years[val[0]],
      checkedMonth: this.data.months[val[1]],
    })    
  },
  /*月份选择器-end*/
})  
css 复制代码
/*选择月份-start*/
.fixed-select {
  position: fixed;
  bottom: 0;
  left: 0;
  z-index: 10;
  height: 350rpx;
  background-color: #fff;    
  border-top: 2px solid #e9e9ea;
}

.fixed-select .select-head {
  position: absolute;
  top: 0;
  z-index: 111;  
  display: flex;  
  justify-content: space-between;
  width: 100%;
  line-height: 60rpx;
  background-color: #eee;
}

.fixed-select .select-head .close {
  padding: 10rpx 20rpx;
}

.fixed-select .select-head .sure {
  color: #ffb81c;
}
.picker-list{line-height: 50px; text-align: center;}
/*选择月份-end*/

自己记录用的 可以作为参考

相关推荐
游戏开发爱好者82 小时前
日常开发与测试的 App 测试方法、查看设备状态、实时日志、应用数据
android·ios·小程序·https·uni-app·iphone·webview
2501_915106324 小时前
app 上架过程,安装包准备、证书与描述文件管理、安装测试、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_915106324 小时前
使用 Sniffmaster TCP 抓包和 Wireshark 网络分析
网络协议·tcp/ip·ios·小程序·uni-app·wireshark·iphone
宠友信息5 小时前
2025社交+IM及时通讯社区APP仿小红书小程序
java·spring boot·小程序·uni-app·web app
“负拾捌”6 小时前
python + uniapp 结合腾讯云实现实时语音识别功能(WebSocket)
python·websocket·微信小程序·uni-app·大模型·腾讯云·语音识别
换日线°1 天前
NFC标签打开微信小程序
前端·微信小程序
光影少年1 天前
AIGC + Taro / 小程序
小程序·aigc·taro
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
浮桥1 天前
uniapp+h5 公众号实现分享海报绘制
uni-app·notepad++
2501_916007471 天前
没有 Mac 用户如何上架 App Store,IPA生成、证书与描述文件管理、跨平台上传
android·macos·ios·小程序·uni-app·iphone·webview