• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

微信开发

微信小程序复选框完成多选一功用

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/weixinkaifa/2019/0504/483.html

 
功用完成界面data: { checkboxItems: }想要完成的功用四个复选框中只能选一个,且选中另一个会取消其他选中,且能保管选择的value值JS代码完成checkboxChange: function (e) { var that = this; let checkboxValues= ...
 
游戏开发运营功用完成界面
 
 
复制代码
data: {
    checkboxItems: [
      { name: '全天(1-8节)', value: 'allday' },
      { name: '上午(1-4节)', value: 'am' },
      { name: '下午(5-8节)', value: 'pm' },
      { name: '晚上(自修)', value: 'night' },
    ]
  }
复制代码
 
 
 
 
想要完成的功用
 
 
四个复选框中只能选一个,且选中另一个会取消其他选中,且能保管选择的value值
JS代码完成
 
复制代码
checkboxChange: function (e) {
    var that = this;
    let checkboxValues=null;
    let checkboxItems = this.data.checkboxItems, values = e.detail.value
    for (var i = 0, lenI = checkboxItems.length; i < lenI; ++i) {
      if(checkboxItems[i].value==values[values.length-1]){
        checkboxItems[i].checked=true;
        checkboxValues = checkboxItems[i].value;
      }
      else{
        checkboxItems[i].checked = false;
      }
    }
    console.log(checkboxValues)
    that.setData({ checkboxItems, checkboxValues })
  }
复制代码
 
 
 
 
常州手游开发前端代码
 
 
复制代码
<view class="weui-cells weui-cells_after-title">
      <checkbox-group class="weui-flex" bindchange="checkboxChange">
        <label class="weui-cell weui-check__label weui-flex__item" wx:for="{{checkboxItems}}" wx:key="value">
          <checkbox class="weui-check" value="{{item.value}}" checked="{{item.checked}}" />
          <view class="weui-cell__hd weui-check__hd_in-checkbox">
            <icon class="weui-icon-checkbox_circle" type="circle" size="23" wx:if="{{!item.checked}}"></icon>
            <icon class="weui-icon-checkbox_success" type="success" size="23" wx:if="{{item.checked}}"></icon>
          </view>
          <view class="weui-cell__bd">{{item.name}}</view>
        </label>
      </checkbox-group>
    </view>
复制代码
对应的CSS款式是

上篇:上一篇:js保存两位小数要领总结
下篇:下一篇:将异步回调接口 Promise 化