【如何获取到picker的index _小程序语言基础 _小程序界面构建 】 | IT修真院·坑乎
问题已收录 如何获取到picker的index
我也踩过这个坑( 1 )
已统计您的踩坑,无需重复点击
回答(1)
小程序语言基础 小程序界面构建
详细描述
截图
代码
编辑于2024-05-17
  • [西安|荣耀师兄]JS-许恒倩
    0

    wxml:

    <picker wx:for="{{score}}"  data-current="{{index}}" bindchange="bindPickerChange" value="{{item.index}}" range="{{item.option}}" range-key="{{'name'}}">

    <view class="picker">
    {{item.name}}{{item.option[item.index].name}}
    </view>

    js:

    bindPickerChange: function(e) {

    // 定义一个变量curindex 储存触发事件的数组对象的下标
    const curindex = e.target.dataset.current;
    // 根据下标 改变该数组对象中的index值
    this.data.score[curindex].index = e.detail.value;
    // console.log(curindex);
    this.setData({
    // 把改变某个数组对象index值之后的全新score重新 赋值给score
    score: this.data.score,

    })

    scores是从官网中拷贝下来的选项数组,给数组中每个选项增加一个index属性,以便在picker选项选择后把e.detail.value赋给index

    onLoad: function(options) {
    this.data.score.map(item => {
    item.index = 0
    })
    this.setData({
    score: this.data.score,
    })


    编辑于2018-11-17