微信小程序动态修改data里面的值

wxml

1
2
3
4
5
6
<view>
<block wx:for="{{test}}" wx:key="*this">
<button type='primary' size='mini' bindtap='chageval' data-id='{{item.id}}'>{{item.name}}</button>
<text>{{item.val}}</text>
</block>
</view>

js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
test: [
{
id: 1,
name: 'wangyu',
val: '123'
},
{
id: 2,
name: 'mayan',
val: '456'
}
]
chageval(e){
var x = e.currentTarget.dataset.id;
console.log(x);
var str = "test[" + (x-1) + "].val";
this.setData({
[str]: 'aaa'+x
});
}

img