微信小程序 单击按钮后多添加一个输入框
首先 WXML
中要设置循环,实现动态的添加和删除
<view>
<view class="lines">
<button class="btn1" bindtap="add">添加</button>
<button class="btn1" bindtap="delete">删除</button>
</view>
<view wx:for="{{reactList}}" wx:key="{{index}}" class="choose">
<input type="text" placeholder="选项" class="in" data-id="{{index}}"/>
</view>
</view>
样式表WXSS
.btn1 {
color: white;
background-color: thistle;
}
.btn1:active{
background-color: turquoise;
}
.lines{
margin-top: 40rpx;
display: flex;
flex-direction: row;
}
.in{
box-sizing: border-box;
width: 240rpx;
height: 100rpx;
background-color: wheat;
}
.choose{
margin-top: 20rpx;
}
js
文件内容
data: {
reactList:[{}]
},
add:function(e){
let _list = this.data.reactList;
_list.push({});
this.setData({
reactList : _list
})
},
//删除第一种写法
delete1:function(e){
let _list = this.data.reactList;
_list.pop();
this.setData({
reactList:_list
})
},
//删除第二种写法
delete2:function(e){
let _list = this.data.reactList;
let _index = e.target.dataset.index;
_list.splice(_index,1);
this.setData({
reactList:_list
})
},