126 字
1 分钟
Vue3动态添加输入框并添加规则校验
前言
利用Vue 3的响应式能力和组合式API,可以轻松实现动态添加输入框及其实时校验。通过v-model绑定输入框值,结合自定义规则与watch监听数据变化,开发者能够灵活地扩展表单功能并确保输入内容的有效性。本文将简明介绍如何运用Vue 3实现这一过程,让您的应用交互更加智能高效。
实现步骤
<template> <div> <el-form :model="info" ref="forms"> <el-table ref="tableRef" :data="info.data" border> <el-table-column align="center" property="name" label="*姓名"> <template #default="row"> <el-form-item :prop="'data.' + row.$index + '.name'" :rules="formRules.name"> <el-input placeholder="请输入姓名" v-model="info.data[row.$index].name" /> </el-form-item> </template> </el-table-column> <el-table-column align="center" property="role" label="角色"> <template #default="row"> <el-form-item :prop="'data.' + row.$index + '.role'" :rules="formRules.role"> <el-input placeholder="请输入角色" v-model="info.data[row.$index].role" /> </el-form-item> </template> </el-table-column> </el-table> </el-form> <el-button type="primary" @click="submitForm()">Submit</el-button> </div></template><script setup lang="ts">import {ref, reactive} from 'vue'import type { FormInstance } from 'element-plus'let info:any = reactive({ data:[ { id: 0, name: '', role:'' },{ id: 1, name: '', role:'' } ]})const formRules = reactive({ name: [{ required: true, message: '请输入姓名', trigger: 'change' }], role: [{ required: true, message: '请输入角色', trigger: 'change' }]})const forms = ref<FormInstance>()const submitForm = async () => { if (!forms) return return await forms.value?.validate((valid: any) => { if (valid) { console.log('submit!') } else { console.log('error submit!') return false } })}</script> 分享
如果这篇文章对你有帮助,欢迎分享给更多人!
Vue3动态添加输入框并添加规则校验
https://blog.olinl.com/posts/vue3-dynamic-validate/ 相关文章 智能推荐
1
Vue3使用el-upload上传图片
代码开发实战 在Vue3 中使用 el-upload 组件上传图片的案例。
2
v-table组件添加序号
代码开发实战 在Vue3 中使用 el-table 组件添加序号的案例。
3
Vue3使用flv.js播放FLV视频流
代码开发实战 在Vue3 中使用 flv.js 播放 FLV 在线流的案例。
4
Nginx 使用 sub_filter 注入自定义 HTML 标签
服务与应用运维 通过 Nginx 的 ngx_http_sub_module 模块,在反向代理响应中注入自定义 JS、CSS 或 HTML 标签,适用于无法修改源码的第三方页面定制场景。
5
为了护住我那几块硬盘:我的 UPS 监控“三部曲
HomeLab 私有云 从 PVE 备注里的简陋脚本,到独立的 Node.js Web 监控页,记录一个 HomeLab 玩家的 UPS 监控进阶之路。
