编程语言
首页 > 编程语言> > 小程序下拉框

小程序下拉框

作者:互联网

页面

 <view class="info info-name">
				<label class="info-lable">所属街道</label>
		
				<picker bindchange="bindPickerChange2" range-key='{{"cboText"}}' style="float:left; margin-left: 5%" range="{{AreaCode}}">
					<view>
						<text style="color:#848484">{{AreaCode[qx].cboText}}</text>
					</view>
				</picker>
			</view>

JS

data{
  AreaCode: [{
      cboValue: '',
      cboText: '请选择区县'
    }],
    selectQA: '',
    qx:0
    }

onload:function(options)
{
this.GetAreaCode()//调用查询方法
//回填下拉框数值
查询成功后再setData中赋值
this.setData({
selectQA:.areaCode,
})
}
  GetAreaCode: function () {

    var that = this;
    wx.request({
      url: app.globalData.ApiPath + 'api/Common/GetComAreaCodeList',
      method: 'post',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {

        that.setData({
          ['AreaCode[0].cboValue']: '',
          ['AreaCode[0].cboText']: '请选择区县'
        })

        for (var i = 0; i < res.data.length; i++) {
       
          var cboValue = 'AreaCode[' + (i + 1) + '].cboValue';
          var cboText = 'AreaCode[' + (i + 1) + '].cboText';
          //console.log(cboValue,cboText)
          that.setData({
            [cboValue]: res.data[i].areaCode,
            [cboText]: res.data[i].areaName
          })
        }
        if (that.data.selectQA != "") {
          for (let i = 0; i < that.data.AreaCode.length; i++) {
            if (that.data.AreaCode[i].cboValue == that.data.selectQA) {
              that.setData({
                qx: i
              })
            }
          }
        }
        //console.log(that.data.AreaCode)
      }
    })
  },
  //获取下拉框值
   bindPickerChange2: function (e) {

    //console.log('picker发送选择改变,携带值为', e.detail.value)
    var areacode = this.data.AreaCode[e.detail.value].cboValue;
    this.setData({
      qx: e.detail.value,
      selectQA: areacode
    })
   
  },

标签:AreaCode,cboText,程序,cboValue,var,data,下拉框,setData
来源: https://blog.csdn.net/gx18331567741/article/details/121925478