axml
<picker onChange="bindObjPickerChange" value="{{arrIndex}}" range="{{objectArray}}" range-key="name">
<view class="row">
<view class="row-title">证件类型</view>
<view class="row-extra">当前选择:{{objectArray[arrIndex].name}}</view>
<image class="row-arrow" src="/image/arrowright.png" mode="aspectFill" />
</view>
</picker>
data
data: {
cardType: '身份证',
objectArray: [{id: 0,name: '身份证'},{id: 1,name: '护照'},{id: 2,name: '其他证件'}],
arrIndex: 0,
},
js
bindObjPickerChange(e) {
// 方式一 适合简单对象
console.log('picker发送选择改变,携带值为', e.detail.value);
this.setData({
arrIndex: e.detail.value,
});
if(e.detail.value==0) this.setData({cardType:'身份证'});
if(e.detail.value==1) this.setData({cardType:'护照'});
if(e.detail.value==2) this.setData({cardType:'其他证件'});
// 方式二 适合复杂对象获取
let obj = this.data.objectArray[e.detail.value]
this.setData({
arrIndex: e.detail.value,
});
注: 0bj就是这个复制的对象
},