微信小程序是没有双向数据绑定的,只有vue才有
双向数据绑定的原理:就是value 加上 input输入事件的结合
<view wx:for="{{inputArray}}" wx:key="index">
<input value="{{item.value}}" data-index="{{index}}" bindinput="inputChange" />
</view>
<script>
Page({
data: {
inputArray: [
{ value: 'Input 1' },
{ value: 'Input 2' },
{ value: 'Input 3' }
]
},
inputChange: function (e) {
const index = e.currentTarget.dataset.index;
const value = e.detail.value;
const inputArray = this.data.inputArray;
// 更新对应索引的input的值
inputArray[index].value = value;
// 更新数据源
this.setData({
inputArray: inputArray
});
}
});
</script>