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

页面代码,整体代码稍后打包
<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>