当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 小程序实现单选多选功能

小程序实现单选多选功能

2018年11月26日  | 移动技术网IT编程  | 我要评论

小程序的单选组件radio和多选组件checkbox的样式只提供更改颜色,这对实际项目中的需求显然是不够的,所以自己模拟实现一个。

踩坑点:小程序不支持操作dom

1、模拟实现多选框:

实现思路:思路非常简单,给每个选项绑定checked属性,类型为布尔值,点击取反即可

<!--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
 },
})

2、模拟实现单选框

思路:这个和多选差不多,区别就是需要在点击时清空其他项的选中状态,然后再把当前项设置为选中状态

代码也差不多

wxml的话就把check-group标签改为radio-group; js那边就在点击时多加个判断

<!--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>

 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
 },
})

最后上个效果截图


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网