weixin_52098007 2022-10-20 09:49 采纳率: 60%
浏览 87
已结题

这个怎么回事呢呢,想听取一下意见🙏🏻

img


在uniapp中,当点击按钮时,获取所有复选框组选中项,并转换成JSON格式字符串在控制台输出

  • 写回答

3条回答 默认 最新

  • 游一游走一走 2022-10-20 10:26
    关注

    效果图

    img

    页面代码,整体代码稍后打包

    <template>
        <view>
            <view class="uni-panel-h" v-for="(i, index) in alarmList" :key="i.value" @click="checkAlarm(index)">
                <checkbox :checked="i.isChecked" />
                <text>{{i.name}}</text>
            </view>
            <button @click="printData" class="button01">提交数据</button>
        </view>
    </template>
    <style>
        .button01 {
            width: 80%;
            margin: auto;
        }
    </style>
    <script>
        export default {
            data() {
                return {
                    alarmList: [{
                            name: '美国',
                            value: '美国',
                            isChecked: false
                        },
                        {
                            name: '中国',
                            value: '中国',
                            isChecked: true
                        },
                        {
                            name: '巴西',
                            value: '巴西',
                            isChecked: true
                        },
                        {
                            name: '日本',
                            value: '日本',
                            isChecked: false
                        },
                        {
                            name: '英国',
                            value: '英国',
                            isChecked: false
                        },
                        {
                            name: '法国',
                            value: '法国',
                            isChecked: false
                        },
                    ]
                }
            },
            methods: {
                checkAlarm(index) {
                    var invoice = this.alarmList[index]; //变量 invoice == 数组每一项索引
                    if (invoice) {
                        invoice.isChecked = !invoice.isChecked //取反
                    }
                },
                printData() {
                    console.log(JSON.stringify(this.alarmList.filter(item => item.isChecked)))
                }
    
            }
        }
    </script>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 10月28日
  • 已采纳回答 10月20日
  • 修改了问题 10月20日
  • 修改了问题 10月20日
  • 展开全部