• uniapp多选按钮


    闲言少叙,直接上效果图,看图才知道是不是自己想要的效果

     HTML代码

    <view class="page index">
            <!-- 多选,不改变选择中后的值 -->
            <view class="list-box">
                <view v-for="(item,index) in list" :key="index" @click="choice(index)" :class="[item.selected?'selde':'noselde']">
                    {{item.selected?item.title:item.title}}
                </view>
            </view>
    
            <!-- 多选,改变选择中后的值 -->
            <!-- <view class="list-box">
                <view v-for="(item,index) in list" :key="index" @click="choice(index)" :class="[item.selected?'selde':'noselde']">
                    {{item.selected?"已选择":"选择"}}
                </view>
            </view> -->
        </view>

    js代码

    <script>
        export default {
            data() {
                return {
                    list: [{
                            selected: false,
                            title: '张三'
                        },
                        {
                            selected: false,
                            title: '李四'
                        },
                        {
                            selected: false,
                            title: '张三'
                        },
                        {
                            selected: false,
                            title: '张三'
                        },
                        {
                            selected: false,
                            title: '张三'
                        },
                    ],
                    selectId: [],
                };
            },
            methods: {
                //选择按钮
                choice(index) {
                    if (this.list[index].selected == true) {
                        this.list[index].selected = false;
                        //取消选中时删除数组中的值
                        for (var i = 0; i < this.selectId.length; i++) {
                            if (this.selectId[i] === this.list[index].course_id) {
                                this.selectId.splice(i, 1);
                            }
                        }
                        console.log("选中的值", this.selectId)
                    } else {
                        this.list[index].selected = true;
                        this.selectId.push(this.list[index].course_id)
                        console.log("选中的值", this.selectId)
                    }
                }
            }
        };
    </script>

    scss代码

    <style lang="scss">
        .list-box {
            display: flex;
            flex-wrap: wrap;
            padding: 16upx;
            border-radius: 10upx;
    
            view {
                width: 30%;
                height: 60upx;
                line-height: 60upx;
                text-align: center;
                margin-top: 30upx;
    
                &:not(:nth-child(3n)) {
                    margin-right: calc(10% / 2);
                }
            }
        }
    
        /* 已选择 */
        .selde {
            border: 1px solid red;
            background: red;
            color: #FFFFFF;
            border-radius: 20upx;
            font-size: 20upx;
            padding: 0 10upx;
        }
    
        /* 未选择 */
        .noselde {
            border: 1px solid #959595;
            background: #FFFFFF;
            color: #959595;
            border-radius: 20upx;
            font-size: 20upx;
            padding: 0 10upx;
        }
    </style>
  • 相关阅读:
    PacificA协议小结
    raft协议小结
    python爬虫抓取图片
    composer 的使用和常用命令大全
    php批量同步数据
    VMware虚拟机的安装与配置
    国家和地区代码表
    js判断h5页面地址的打开方式(微信、pc、移动端)
    phpexcel图片获取
    python的文件操作及简单的用例
  • 原文地址:https://www.cnblogs.com/cqiong/p/14442508.html
Copyright © 2020-2023  润新知