• 微信小程序 单选按钮 最佳


    wxml:

    <view class='button_container'>
    <block wx:for="{{buttons}}" wx:key="buttons">
    <button class='{{item.checked?"checked_button":"normal_button"}}' data-id='{{item.id}}' bindtap='radioButtonTap'>{{item.name}}</button>
    </block>
    </view>
     
    js;
    data:{
    buttons: [{ id: 1, name: '失物招领' }, { id: 2, name: '寻物启事' }]
    }
    onLoad: function() {//默认选了第一个
    this.data.buttons[0].checked = true;
    this.setData({
    buttons: this.data.buttons,
    })
    }
     
    radioButtonTap: function (e) {
    console.log(e)
    let id = e.currentTarget.dataset.id
    console.log(id)
    for (let i = 0; i < this.data.buttons.length; i++) {
    if (this.data.buttons[i].id == id) {
    //当前点击的位置为true即选中
    this.data.buttons[i].checked = true;
    }
    else {
    //其他的位置为false
    this.data.buttons[i].checked = false;
    }
    }
    this.setData({
    buttons: this.data.buttons
    })
    },
     
    css:
    .button_container{
    display: flex;
    flex-direction: row;
    justify-content: space-around
    }
    /* 按钮未选中 */
    .normal_button{
    background: white;
    }
    /* 按钮选中 */
    .checked_button{
    background: #36ab60;
    color: white
    }
  • 相关阅读:
    Nginx负载均衡配置实例详解
    Tomcat服务器
    如何设计安全的用户登录功能
    Tomcat 配置用户认证服务供C#客户端调用
    DataWindow值获取
    弹出上下文菜单
    DataWindow快速从Grid格式转为Freefrom
    postEvent() @ triggerEvent
    日期赋值为: 0000:00:00
    截取字符串
  • 原文地址:https://www.cnblogs.com/lsyy2017/p/11564524.html
Copyright © 2020-2023  润新知