🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
``` <template> <view> <uni-section title="多选" type="line"></uni-section> <uni-group title="爱好"> <view class="text">选中:{{JSON.stringify(role)}} {{role}}</view> <uni-data-checkbox multiple v-model="role" :localdata="roles"></uni-data-checkbox> </uni-group> <uni-section title="单选" type="line"></uni-section> <uni-group title="性别"> <view class="text">选中:{{formData.sex}}</view> <uni-data-checkbox v-model="formData.sex" :localdata="roles"></uni-data-checkbox> </uni-group> <view class="uni-button-group"> <view class="uni-btn-v uni-common-mt" style="margin-left:30upx;margin-right:30upx;"> <button class="btn-submit" type="warn" @tap="submit">提交</button> </view> </view> </view> </template> <script> var api = require('@/common/api.js') export default { data() { return { formData: [], sex: [{ text: '男', value: 0 }, { text: '女', value: 1 }, { text: '未知', value: 2 }], role: '', roles: [], } }, onLoad(e) { const id = e.id this.formDataId = id this.getDetail(id) this.loadroles() }, onReady() {}, methods: { submit() { api.put({ // url: 'portal/articles/' + this.id, url: 'order/items/4', data: { sex: this.formData.sex, // role: JSON.stringify(value(this.role)) role: this.role.join(',') // role: this.formData.role }, success: (data) => { //console.log(data) if (data.code == 1) { uni.showToast({ title: data.msg, icon: "success", duration: 1000 }) setTimeout((e => { uni.navigateBack(); }), 1000); } if (data.code == 0) { uni.showToast({ title: data.msg, icon: "none", duration: 1000 }) } } }) }, loadroles() { api.get({ url: 'order/categories', data: { order: '+id', field: 'id,name' }, success: data => { const roleIds = [] this.roles = data.data.map(item => { roleIds.push(item.id) return { value: item.id, role: item.id, text: item.name } }) console.log(this.roles); console.log(roleIds); } }); }, /** * 获取表单数据 * @param {Object} id */ getDetail(id) { uni.showLoading({ mask: true }) api.get({ url: 'order/items/4', //url: 'order/items/' + event.id, success: result => { //console.log(result); if (result.code == 1) { var res = result.data this.formData = res console.log(this.formData) //方案1 ok var rolearr = res.role.split(",") var roles = rolearr.map(Number) this.role = roles console.log(this.role) //方案2 var dataStr = res.role;//原始字符串 var dataStrArr=dataStr.split(",");//分割成字符串数组 var dataIntArr=[];//保存转换后的整型字符串 //方法一 dataStrArr.forEach(function(data,index,arr){ dataIntArr.push(+data); }); console.log(dataIntArr); //方法二 dataIntArr=dataStrArr.map(function(data){ return +data; }); console.log(dataIntArr); uni.hideLoading() } // this.formData.hobby = res.role //roleIds.push(item.id) } }); }, } } </script> <style> @charset "UTF-8"; /* 头条小程序组件内不能引入字体 */ /* #ifdef MP-TOUTIAO */ @font-face { font-family: uniicons; font-weight: normal; font-style: normal; src: url("~@/static/uni.ttf") format("truetype"); } /* #endif */ /* #ifndef APP-NVUE */ page { display: flex; flex-direction: column; box-sizing: border-box; background-color: #efeff4; min-height: 100%; height: auto; } view { font-size: 14px; line-height: inherit; } .example { padding: 0 15px 15px; } .example-info { padding: 15px; color: #3b4144; background: #ffffff; } .example-body { /* #ifndef APP-NVUE */ display: flex; /* #endif */ flex-direction: row; flex-wrap: wrap; justify-content: center; padding: 0; font-size: 14px; background-color: #ffffff; } /* #endif */ .example { padding: 0 15px; } .example-info { /* #ifndef APP-NVUE */ display: block; /* #endif */ padding: 15px; color: #3b4144; background-color: #ffffff; font-size: 14px; line-height: 20px; } .example-info-text { font-size: 14px; line-height: 20px; color: #3b4144; } .example-body { flex-direction: column; padding: 15px; background-color: #ffffff; } .word-btn-white { font-size: 18px; color: #FFFFFF; } .word-btn { /* #ifndef APP-NVUE */ display: flex; /* #endif */ flex-direction: row; align-items: center; justify-content: center; border-radius: 6px; height: 48px; margin: 15px; background-color: #007AFF; } .word-btn--hover { background-color: #4ca2ff; } .example { padding: 10px; background-color: #fff; } .text { font-size: 14px; color: #333; margin-bottom: 10px; } </style> ```