小程序下拉框赋值获取id和title?
发布于 7 年前 作者 cqian 13036 次浏览 来自 官方Issues

废话不多说上代码

wxml代码

<view class=“name”>

<view>品牌:</view>

<view>

<picker bindchange=“changeBrandType” value="{{indexBrandType}}" range="{{arrayBrandType}}" range-key="{{‘title’}}" >

{{arrayBrandType[indexBrandType].title}}

</picker>

</view>

</view>

.js代码

/**

* 页面的初始数据

*/

data: {

arrayBrandType: [{ title: ‘服务质量’, id: 1 }, { title: ‘服务品质’, id: 2 }, { title: ‘服务速度’, id: 25 }],

indexBrandType: 0, //值 同步的 显示的

},

/**

* 生命周期函数–监听页面加载

*/

onLoad: function (options) {

var that = this;

wx.request({

url: app.globalData.wPathApi + ‘GetBrandType.ashx’, data: {

strkey: app.globalData.strkey

},

header: { ‘content-type’: ‘application/json’ },

success: function (res) {

//console.log(res)arrayBrandTypeId

if (res.data.Result === 1) {

//赋值

that.setData({

arrayBrandType: res.data.Data

})

setTimeout(function () {

wx.hideLoading();

}, 1000)

}

else {

//

}

//

}

})

},

// 改变下拉选项

changeBrandType: function (event) {

var that = this;

this.setData({ //给变量赋值

indexBrandType: event.detail.value,

})

console.log(‘乔丹选的是’, event.detail.value)//正常索引正常赋值

console.log(‘乔丹选的是’, this.data.arrayBrandType[event.detail.value].title)

console.log(‘乔丹选的是’, this.data.arrayBrandType[event.detail.value].id)

}

}

1 回复

changeBrandType:function(e){

    console.log(e)

}

回到顶部