小程序的單選組件radio和多選組件checkbox的樣式只提供更改顏色,這對實際項目中的需求顯然是不夠的,因此本身模擬實現一個。小程序
踩坑點:小程序不支持操做domdom
一、模擬實現多選框:xss
實現思路:思路很是簡單,給每一個選項綁定checked屬性,類型爲布爾值,點擊取反便可this
<!--wxml--> <view class='wrap'> <view class='checkbox-con'> <checkbox-group bindchange="checkboxChange"> <label class="{{item.checked?'checkbox checked':'checkbox'}}" wx:for="{{checkboxArr}}" bindtap='checkbox' data-index="{{index}}" wx:key="item.name"> <checkbox value="{{item.name}}" checked="{{item.checked}}"/>{{item.name}} </label> </checkbox-group> <button type='primary' bindtap='confirm'>提交</button> </view> </view>
/* wxss */
.wrap{
width: 550rpx;
margin: 50rpx auto
}
.checkbox-con{
margin-top: 40rpx;
text-align: center
}
.checkbox{
width: 260rpx;
height: 72rpx;
line-height: 72rpx;
font-size: 28rpx;
color: #888888;
border: 1rpx solid #CECECE;
border-radius: 5rpx;
display: inline-block;
margin: 0 10rpx 20rpx 0;
position: relative
}
.checked{
color: #1A92EC;
background: rgba(49,165,253,0.08);
border: 1rpx solid #31A5FD;
}
.checkbox checkbox{
display: none
}
.checked-img{
width: 28rpx;
height: 28rpx;
position: absolute;
top: 0;
right: 0
}
//js
Page({
data: {
checkboxArr: [{
name: '選項A',
checked: false
}, {
name: '選項B',
checked: false
}, {
name: '選項C',
checked: false
}, {
name: '選項D',
checked: false
}, {
name: '選項E',
checked: false
}, {
name: '選項F',
checked: false
}],
},
checkbox: function (e) {
var index = e.currentTarget.dataset.index;//獲取當前點擊的下標
var checkboxArr = this.data.checkboxArr;//選項集合
checkboxArr[index].checked = !checkboxArr[index].checked;//改變當前選中的checked值
this.setData({
checkboxArr: checkboxArr
});
},
checkboxChange: function (e) {
var checkValue = e.detail.value;
this.setData({
checkValue: checkValue
});
},
confirm: function() {// 提交
console.log(this.data.checkValue)//全部選中的項的value
},
})
效果展現圖:spa
二、模擬實現單選框.net
思路:這個和多選差很少,區別就是須要在點擊時清空其餘項的選中狀態,而後再把當前項設置爲選中狀態code
代碼也差很少xml
wxml的話就把check-group標籤改成radio-group; js那邊就在點擊時多加個判斷htm
<!--wxml--> <view class='wrap'> <view class='checkbox-con'> <radio-group bindchange="radioChange"> <label class="{{item.checked?'checkbox checked':'checkbox'}}" wx:for="{{checkboxArr}}" bindtap='radio' data-index="{{index}}" wx:key="item.name"> <checkbox value="{{item.name}}" checked="{{item.checked}}"/>{{item.name}} </label> </radio-group> <button type='primary' bindtap='confirm'>提交</button> </view> </view>
//js
Page({
data: {
checkboxArr: [{
name: '選項A',
checked: false
}, {
name: '選項B',
checked: false
}, {
name: '選項C',
checked: false
}, {
name: '選項D',
checked: false
}, {
name: '選項E',
checked: false
}, {
name: '選項F',
checked: false
}],
},
radio: function (e) {
var index = e.currentTarget.dataset.index;//獲取當前點擊的下標
var checkboxArr = this.data.checkboxArr;//選項集合
if (checkboxArr[index].checked) return;//若是點擊的當前已選中則返回
checkboxArr.forEach(item => {
item.checked = false
})
checkboxArr[index].checked = true;//改變當前選中的checked值
this.setData({
checkboxArr: checkboxArr
});
},
radioChange: function (e) {
var checkValue = e.detail.value;
this.setData({
checkValue: checkValue
});
},
confirm: function() {// 提交
console.log(this.data.checkValue)//全部選中的項的value
},
})
效果展現圖:blog
https://www.jb51.net/article/150127.htm