解决方案
先用一个字符串变量表示var key = 'keys.user_no';
再将setData中该字符串变量用[ ]括起来。[key]
代码示例
一:data数据【对象】
data: {
keys:{
user_no: 1,
password: 2
}
},
设置操作
bindKeyInputuserno: function (a) {
var key = 'keys.user_no'
this.setData({
[key]: a.detail.value,
})
console.log(this.data.keys.user_no)
},
bindKeyInputpassword: function (a) {
var key = 'keys.password'
this.setData({
[key]: a.detail.value,
})
二:data数据【对象】
Page({
data: {
list: [
{
id: 0,
val: ""
},
{
id: 1,
val: ""
},
{
id: 2,
val: ""
}
]
}
})
设置操作
bindKeyInputuserno: function (a) {
var key = 'list[0].id'
this.setData({
[key]: a.detail.value,
})
console.log(this.data.keys.user_no)
},
bindKeyInputpassword: function (a) {
var key = 'list[0].val'
this.setData({
[key]: a.detail.value,
})
标签:no,keys,微信,中用,key,var,data,setData
来源: https://blog.csdn.net/firstday_willow/article/details/104801894