vue 坑 checked 和v-model共用

本文探讨了在Vue中使用v-model绑定变量控制checkbox的行为。当一个checkbox元素与v-model绑定时,其value属性由绑定的变量控制,而checked属性将失效。文章通过示例代码展示了如何仅通过修改myName变量来控制哪些选项被选中。

input type=checkbox 当使用v-model绑定某个变量了 ,只能通过绑定的这个变量来控制改input的value, 当:checked同时存在时 后者将无效;

html
<input type="checkbox" value="Jack" v-model="myName" :checked="1==1?'checked':''">
<input type="checkbox" value="Kindle" v-model="myName">
js 

data :function(){
    return myName:['Kindle'],
}    

此时只有第二个input备选,:checked不起作用,只能通过改变myName来控制哪一个需要被选择

  

转载于:https://www.cnblogs.com/xhliang/p/9856735.html

import type { VbenFormSchema as FormSchema, VbenFormProps, } from '@vben/common-ui'; import type { ComponentType } from './component'; import { setupVbenForm, useVbenForm as useForm, z } from '@vben/common-ui'; import { $t } from '@vben/locales'; async function initSetupVbenForm() { setupVbenForm<ComponentType>({ config: { // ant design vue组件库默认都是 v-model:value baseModelPropName: 'value', // 一些组件是 v-model:checked 或者 v-model:fileList modelPropNameMap: { Checkbox: 'checked', Radio: 'checked', Switch: 'checked', Upload: 'fileList', }, }, defineRules: { // 输入项目必填国际化适配 required: (value, _params, ctx) => { if (value === undefined || value === null || value.length === 0) { return $t('ui.formRules.required', [ctx.label]); } return true; }, // 选择项目必填国际化适配 selectRequired: (value, _params, ctx) => { if (value === undefined || value === null) { return $t('ui.formRules.selectRequired', [ctx.label]); } return true; }, }, }); } const useVbenForm = useForm<ComponentType>; export { initSetupVbenForm, useVbenForm, z }; export type VbenFormSchema = FormSchema<ComponentType>; export type { VbenFormProps }; 根据这个封装的form使用 const [Form, formApi] = useVbenForm({ showDefaultActions: false, // 所有表单项共用,可单独在表单内覆盖 commonConfig: { // 所有表单项 componentProps: { class: "w-full", }, }, // 提交函数 handleSubmit: onSubmitModel, // 垂直布局,labelinput在不同行,值为vertical // 水平布局,labelinput在同一行 // showDefaultActions:false, // scrollToFirstError: true, layout: "vertical", schema: [ { component: "Select", componentProps: { allowClear: true, filterOption: true, options: [ { label: "选项1", value: "1", }, { label: "选项2", value: "2", }, ], placeholder: $t("page.all.select"), showSearch: true, }, defaultValue: undefined, fieldName: "group_codes", label: $t("page.all.agent"), rules: "selectRequired", }, { component: "Upload", componentProps: { allowClear: true, filterOption: true, showSearch: true, }, fieldName: "group_codes", label: $t("page.all.device_id"), rules: "uploadRequired", }, ], wrapperClass: "grid-cols-1", }); 怎么Upload才正确
07-17
<!-- 一般事项详情页 --> <template> <div class="content"> <div class="reportTime"> <p class="title">上报时间</p> <p class="contentText">{{ sj ? sj : nowTime }}</p> <a-divider /> </div> <a-spin :spinning="confirmLoading"> <div class="reportTime"> <!-- @change="handleChange" --> <a-select v-model="formState.noticeCommunityType" style="width: 100%" placeholder="请选择发布性质"> <a-select-option value="信息公示">信息公示</a-select-option> </a-select> </div> <div class="reportTime"> <!-- <p class="title"><span style="color: red">* </span>附件上传</p> <a-upload v-if="!$route.query.type" accept=".jpg,.png,.jpeg,.heic,.raw,.heif" :multiple="false" :headers="headers" :action="action" list-type="picture-card" @change="fileChange($event, i)" @preview="handlePreview" > <div> <a-icon type="plus" /> 选择附件 </div> </a-upload> <div v-else style="display: flex; gap: 10px"> <img style="width: 120px" src="http://ygwy.hdgufei.cn:9000/hdygwy/IMG_20250406_134603238_1743920758142.jpg" alt="" /> <img style="width: 120px" src="http://ygwy.hdgufei.cn:9000/hdygwy/IMG_20250406_134603238_1743920758142.jpg" alt="" /> </div> --> <a-divider /> </div> <!-- 信息公示 --> <div class="reportTime"> <h2 style="color: rgb(31,157,255);">信息公示</h2> <span style="color:#000000">按照《北京市物业管理条例》第70条,物业服务人应当在物业管理区域内显著位置设置公示栏,如实公示、及时更新下列信息,并且可以通过互联网方式告知全体业主。</span> <a-form-model ref="form" :model="model" :rules="validatorRules"> <a-form-model-item prop="publicColumnUrl" :rules="{ required: true, message: '请选择需要上传的文件', }"> <span slot="label"> <span>公示栏(请上传照片,每次上传不要超过9张)</span> <div>(拍照上传)</div> </span> <a-upload @change="handleUploadChange($event, 'publicColumnUrl')" v-if="!$route.query.type" accept=".jpg,.png,.jpeg,.heic,.raw,.heif" list-type="picture-card" :file-list="model.publicColumnUrl" :multiple="true" :headers="headers" :action="action" :before-upload="beforeUploadCheck"> <div> <a-icon type="plus" /> 选择附件 </div> </a-upload> <div v-else style="display: flex; gap: 10px"> <img style="width: 120px" src="http://ygwy.hdgufei.cn:9000/hdygwy/IMG_20250406_134603238_1743920758142.jpg" alt="" /> <img style="width: 120px" src="http://ygwy.hdgufei.cn:9000/hdygwy/IMG_20250406_134603238_1743920758142.jpg" alt="" /> </div> <!-- <a-input v-model="model.lastYear" placeholder="请输入租户名称"></a-input> --> </a-form-model-item> <a-form-model-item prop="otherPublicInfoUrl" :rules="{ required: true, message: '请选择需要上传的文件', }"> <span slot="label"> <span>其他公示信息</span> <div>(拍照上传)</div> </span> <a-upload @change="itemFileChange($event, 'otherPublicInfoUrl')" v-if="!$route.query.type" accept=".jpg,.png,.jpeg,.heic,.raw,.heif" list-type="picture-card" :file-list="model.otherPublicInfoUrl" :multiple="false" :headers="headers" :action="action"> <div> <a-icon type="plus" /> 选择附件 </div> </a-upload> <div v-else style="display: flex; gap: 10px"> <img style="width: 120px" src="http://ygwy.hdgufei.cn:9000/hdygwy/IMG_20250406_134603238_1743920758142.jpg" alt="" /> <img style="width: 120px" src="http://ygwy.hdgufei.cn:9000/hdygwy/IMG_20250406_134603238_1743920758142.jpg" alt="" /> </div> <!-- <a-input v-model="model.lastYear" placeholder="请输入租户名称"></a-input> --> </a-form-model-item> <a-form-model-item prop="incomeDetailUrl" :rules="{ required: true, message: '请选择需要上传的文件', }"> <span slot="label"> <span>上一年度物业服务收支情况明细表</span> <div>(拍照上传)</div> </span> <a-upload @change="itemFileChange($event, 'incomeDetailUrl')" v-if="!$route.query.type" accept=".jpg,.png,.jpeg,.heic,.raw,.heif" list-type="picture-card" :file-list="model.incomeDetailUrl" :multiple="false" :headers="headers" :action="action"> <div> <a-icon type="plus" /> 选择附件 </div> </a-upload> <div v-else style="display: flex; gap: 10px"> <img style="width: 120px" src="http://ygwy.hdgufei.cn:9000/hdygwy/IMG_20250406_134603238_1743920758142.jpg" alt="" /> <img style="width: 120px" src="http://ygwy.hdgufei.cn:9000/hdygwy/IMG_20250406_134603238_1743920758142.jpg" alt="" /> </div> <!-- <a-input v-model="model.lastYear" placeholder="请输入租户名称"></a-input> --> </a-form-model-item> <a-form-model-item prop="incomeStatusUrl" :rules="{ required: true, message: '请选择需要上传的文件', }"> <span slot="label"> <span>物业共用部分经营收支情况表</span> <div>(拍照上传)</div> </span> <a-upload @change="itemFileChange($event, 'incomeStatusUrl')" v-if="!$route.query.type" accept=".jpg,.png,.jpeg,.heic,.raw,.heif" list-type="picture-card" :file-list="model.incomeStatusUrl" :multiple="false" :headers="headers" :action="action"> <div> <a-icon type="plus" /> 选择附件 </div> </a-upload> <div v-else style="display: flex; gap: 10px"> <img style="width: 120px" src="http://ygwy.hdgufei.cn:9000/hdygwy/IMG_20250406_134603238_1743920758142.jpg" alt="" /> <img style="width: 120px" src="http://ygwy.hdgufei.cn:9000/hdygwy/IMG_20250406_134603238_1743920758142.jpg" alt="" /> </div> </a-form-model-item> <a-form-model-item prop="incomeBudgetUrl" :rules="{ required: true, message: '请选择需要上传的文件', trigger: ['change'], }"> <span slot="label"> <span>本年度物业服务项目收支预算</span> <div>(拍照上传)</div> </span> <a-upload @change="itemFileChange($event, 'incomeBudgetUrl')" v-if="!$route.query.type" accept=".jpg,.png,.jpeg,.heic,.raw,.heif" :multiple="false" list-type="picture-card" :file-list="model.incomeBudgetUrl" :headers="headers" :action="action"> <div> <a-icon type="plus" /> 选择附件 </div> </a-upload> <div v-else style="display: flex; gap: 10px"> <img style="width: 120px" src="http://ygwy.hdgufei.cn:9000/hdygwy/IMG_20250406_134603238_1743920758142.jpg" alt="" /> <img style="width: 120px" src="http://ygwy.hdgufei.cn:9000/hdygwy/IMG_20250406_134603238_1743920758142.jpg" alt="" /> </div> </a-form-model-item> </a-form-model> </div> </a-spin> <!-- <div @click="onCheckboxGroupClick"> <a-checkbox-group v-model="checkedList" @change="groupChange" v-if="childTabActive == '1' && paTabActive == 1"> <div v-for="(item, i) in plainOptions" :key="i"> <span class="title middleTitle" v-if="item.title"> {{ item.title }}</span> <br v-if="item.title" /> <a-checkbox v-if="!item.title" :value="item.id" @change="checkBoxChange($event, i)"> <button style="background-color: transparent; border: none; white-space: normal; outline: none; text-align: left" > <span> {{ item.rowNum }}、 {{ item.xxms }} </span> <div v-if="!checkedList.includes(item.id)"> <a-radio-group style="margin-top: 10px" v-model="formModel[i].sblx" @change="radioChange"> <a-radio value="2" style="color: #3b77b3">维护记录</a-radio> <a-radio value="1" style="color: #3b77b3">问题记录</a-radio> </a-radio-group> <div class="itemDetail"> <div class="title"> {{ formModel[i].sblx == 2 ? '维护描述' : '问题描述' }} </div> <div class="discription"> <a-input v-model="formModel[i].wtms" type="textarea" placeholder="请输入" rows="3"></a-input> </div> </div> <div class="itemDetail"> <div class="title"> {{ formModel[i].sblx == 2 ? '维护附件' : '问题附件' }} </div> <div class="discription"> <a-upload accept=".jpg,.png,.jpeg,.heic,.raw,.heif" :multiple="false" :headers="headers" :action="action" list-type="picture-card" @change="fileChange($event, i)" :file-list="formModel[i].wtfj" @preview="handlePreview" > <div> <a-icon type="plus" /> 选择附件 </div> </a-upload> </div> </div> </div> </button> </a-checkbox> </div> </a-checkbox-group> </div> </a-spin> <div class="reportItem" v-if="childTabActive == '2' && paTabActive == 1"> <div class="item" v-for="(item, i) in reprotedData" :key="i"> <p class="title middleTitle" v-if="item.title">{{ item.title }}</p> <div v-if="!item.title"> <div style="display: flex; justify-content: space-between"> <div> <a-icon type="schedule" style="margin-right: 5px; color: #3b77b3" /> {{ item.jgsx ? item.jgsx : '--' }} </div> <a-button style="font-size: 12px; margin: 0" @click="toHandle(item)" shape="round" type="primary" size="small" v-if="item.sblx != 2" >去处置 </a-button> </div> <div class="mainCon">{{ item.xxms }}</div> <div class="title" v-if="item.wtms"> {{ item.sblx == 2 ? '维护描述' : '问题描述' }} </div> <div class="problemCon" v-if="item.wtms"> {{ item.wtms ? item.wtms : '--' }} </div> <div class="title" v-if="item.wtfj"> {{ item.sblx == 2 ? '维护附件' : '问题附件' }} </div> <div style="width: 50px; height: 50px" v-if="item.wtfj"> <img style="width: 50px; height: 50px" :src="item.wtfj" mode="" /> </div> </div> <a-divider /> </div> </div> <a-modal :visible="previewVisible" :footer="null" @cancel="previewVisible = false"> <img alt="example" style="width: 100%" :src="previewImage" /> </a-modal> --> </div> </template> <script> import { getPerformanceById, saveFocusPerformance, savePerformance, getPerById } from '@/api/dailySupervision'; import moment from 'moment'; export default { data() { return { model: { publicColumnUrl: [] // 初始化为空数组 }, formState: { noticeCommunityType: '信息公示' }, sj: "", nowTime: moment(new Date()).format('YYYY年-MM月-DD日 HH:mm:ss'), confirmLoading: false, previewVisible: false, previewImage: '', model: { publicColumnUrl: [], otherPublicInfoUrl: [], incomeDetailUrl: [], incomeStatusUrl: [], incomeBudgetUrl: [] }, validatorRules: { // lastYear: [ // { required: true, message: '请上传附件' } // ], // common: [ // { required: true, message: '请上传附件' } // ], // money: [ // { required: true, message: '请上传附件' } // ] }, headers: { 'X-Access-Token': localStorage.getItem('token') }, action: '/hdygwy/sys/common/upload', headers: { 'X-Access-Token': localStorage.getItem('token') }, action: '/hdygwy/sys/common/upload', childTabActive: this.$route.query.childTabActive, paTabActive: this.$route.query.paTabActive, }; }, props: {}, mounted() { this.getGetPerformanceById(); }, methods: { // 文件上传前的检查 beforeUploadCheck(file, fileList) { // 当前已上传文件数 + 新选文件数 const totalCount = this.model.publicColumnUrl.length + fileList.length; if (totalCount >= 9) { this.$message.error('最多只能上传9张图片'); return false; // 阻止上传 } // 检查文件类型 const validTypes = ['.jpg', '.png', '.jpeg', '.heic', '.raw', '.heif']; const extension = file.name.substring(file.name.lastIndexOf('.')).toLowerCase(); if (!validTypes.includes(extension)) { this.$message.error('不支持的文件格式!'); return false; } return true; }, // 处理文件变化 // handleUploadChange(info, fieldName) { // let fileList = [...info.fileList]; // // 限制最多9个文件 // if (fileList.length > 9) { // fileList = fileList.slice(0, 9); // this.$message.warning('最多只能上传9张图片'); // } // // 更新模型数据 // this.model[fieldName] = fileList; // }, handleUploadChange(info, fieldName) { let fileList = [...info.fileList]; // 检查是否有上传失败的文件 const failedFiles = fileList.filter(file => file.status === 'error'); if (failedFiles.length > 0) { this.$message.error('上传失败!请重新上传'); // 清除上传失败的文件 fileList = fileList.filter(file => file.status !== 'error'); } // 限制最多9个文件 if (fileList.length > 9) { fileList = fileList.slice(0, 9); this.$message.warning('最多只能上传9张图片'); } // 更新模型数据 this.model[fieldName] = fileList; }, // itemFileChange(op, field) { // this.model[field] = op.fileList; // }, itemFileChange(op, field) { const fileList = [...op.fileList]; // 检查是否有上传失败的文件 const failedFiles = fileList.filter(file => file.status === 'error'); if (failedFiles.length > 0) { this.$message.error('上传失败!请重新上传'); // 清除上传失败的文件 this.model[field] = fileList.filter(file => file.status !== 'error'); return; } this.model[field] = fileList; }, // 日常事项提交 wySave() { this.$refs.form.validate(valid => { console.log('valid =>', valid); if (valid) { let params = { "sxlx":2, "xmid": this.$route.query.projectId, "lysxid": this.$route.query.id, "lysxName": this.$route.query.name, "sbsj": moment(new Date()).format('YYYY-MM-DD HH:mm:ss'), "publicColumnUrl": this.model.incomeDetailUrl.map((item) => item.response .message).join(","), "otherPublicInfoUrl": this.model.incomeDetailUrl.map((item) => item.response .message).join(","), "incomeDetailUrl": this.model.incomeDetailUrl.map((item) => item.response .message).join(","), "incomeStatusUrl": this.model.incomeStatusUrl.map((item) => item.response .message).join(","), "incomeBudgetUrl": this.model.incomeBudgetUrl.map((item) => item.response .message).join(",") } savePerformance(params).then((res) => { if (res.success) { this.$message.success(res.message); this.$router.push({ path: "/dailSupervision", query: { paTabActive: this.$route.query.paTabActive, childTabActive: this.$route.query.childTabActive, }, }); } }); } }) // this.spinning = true; // const deptArr = []; // this.formModel.forEach((item) => { // deptArr.push(item); // }); // deptArr.map((item1) => { // if (item1.wtfj) { // item1.wtfj = item1.wtfj[0].response.message; // } // }); // saveFocusPerformance(JSON.stringify(deptArr)).then((res) => { // if (undefined == res) { // this.spinning = false; // } // this.$router.push({ // path: '/dailSupervision' // }); // this.spinning = false; // if (res.success) { // this.$message.success('提交成功'); // } // }); }, getGetPerformanceById() { this.checkedList = []; this.formModel = []; let id = this.$route.query.id ? this.$route.query.id : "83" if (this.$route.query.sxlx == 1) { id = this.$route.query.dkpc; } const params = { id: id, type: this.$route.query.paTabActive, projectId: this.$route.query.projectId, batchKey: this.$route.query.batchKey }; if (this.$route.query.reproted == 1) { getPerformanceById(params).then((res) => { if (res.success) { this.spinning = false; this.checkAttachment = []; const { result } = res; if (result.data) { this.isSnowId = result.data.id; this.datas = result.data; this.xxms = result.data ? result.data.xxms : ""; this.sblx = result.data.sblx; this.sbsj = result.data.sbsj; this.jgsx = result.data.jgsx; this.wtms = result.data.wtms; result.data.wtfj ? result.data.wtfj.split(",").forEach((item) => { this.checkAttachment.push(item); }) : null; } this.jcrxm = result.data.jcrxm; this.jcrdh = result.jcrdh; this.$emit("getCheckPerInfo", this.jcrxm, this.jcrdh); } }); } else { getPerById(params).then((res) => { if (res.success) { this.spinning = false; this.checkAttachment = []; const { result } = res; if (result.data) { this.xxms = result.data ? result.data.xxms : ""; this.sblx = result.data.sblx; this.sbsj = result.data.sbsj; this.jgsx = result.data.jgsx; this.jcrxm = result.jcrxm; this.jcrdh = result.jcrdh; this.$emit("getCheckPerInfo", this.jcrxm, this.jcrdh); } else { this.$message.error(res.message); } } }); } }, onCheckAllChange(e) { this.mainCheck = e.target.checked; if (e.target.checked) { this.checkedList = [...this.checkedListTemp]; if (this.formModel.length != 0) { this.formModel.forEach((item) => { item.sblx = '2'; }); } } else { this.checkedList = []; } }, onCheckboxGroupClick() { this.stopChange += 1; this.stopCheckedList = this.checkedList; }, checkBoxChange(e, i) { // this.formModel[i].sblx = e.target.checked ? 2 : 1; this.formModel[i].sbzt = e.target.checked ? 2 : 0; }, groupChange(e) { if (this.stopChange == 2) { this.checkedList = this.stopCheckedList; return (this.stopChange = 0); } else { this.stopChange = 0; if (e.length != this.plainOptions.length) { this.mainCheck = false; } else { this.mainCheck = true; } } }, toHandle(item) { this.$router.push({ path: '/waitHandle', query: { title: '待处置', id: item.eid, paTabActive: this.$route.query.paTabActive, projectId: this.$route.query.projectId } }); }, fileChange(info, index) { const wtms = this.formModel[index].wtms; this.formModel[index].wtfj = [...info.fileList]; if (this.formModel[index].wtfj.length > 1) { this.formModel[index].wtfj = this.formModel[index].wtfj.slice(-1); } // this.$forceUpdate(); this.formModel[index].wtms = wtms; }, handlePreview(file) { this.previewImage = file.url || file.response.message; this.previewVisible = true; }, radioChange(e) { const isAllChecke = this.formModel.find((item) => item.sblx == '1'); if (!isAllChecke) { this.mainCheck = true; } } } }; </script> <style lang="less" scoped> .content { font-size: 14px; color: #aaa; .reportItem { .item { margin-top: 20px; } } .title { font-weight: 550; margin-bottom: 5px; font-size: 16px; color: #333; margin-top: 15px; } } .middleTitle { margin-top: 20px; } .itemDetail { margin-top: 10px; } /deep/ .ant-checkbox-wrapper { margin-bottom: 10px; margin-left: 0; } /deep/ .ant-btn-primary { background: #3b77b3; border: 1px solid #3b77b3; font-size: 12px; } /deep/ .ant-checkbox-wrapper { display: flex; } /deep/ .ant-checkbox-wrapper>span { display: inline-block; } /deep/ .ant-checkbox-checked { height: 10px !important; } </style> 必填项取消掉可上传可不上传
最新发布
07-31
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值