el-form提交时带默认值
时间: 2023-05-23 14:00:42 浏览: 515
Element-ui 多选.md
可以在el-form中设置默认值,然后在提交时获取表单数据并带上默认值进行提交。以下是示例代码:
<template>
<el-form ref="form" :model="form" :rules="rules">
<el-form-item label="姓名" prop="name">
<el-input v-model="form.name"></el-input>
</el-form-item>
<el-form-item label="年龄" prop="age">
<el-input v-model.number="form.age"></el-input>
</el-form-item>
</el-form>
</template>
<script>
export default {
data() {
return {
form: {
name: 'Tom',
age: 18
},
rules: {
name: [
{ required: true, message: '请输入姓名', trigger: 'blur' }
],
age: [
{ required: true, message: '请输入年龄', trigger: 'blur' },
{ type: 'number', message: '年龄必须为数字值' }
]
}
}
},
methods: {
onSubmit() {
this.$refs.form.validate(valid => {
if (valid) {
// 获取表单数据并带上默认值进行提交
const formData = Object.assign({}, this.form, { gender: 'male' })
// TODO: 发送请求
}
})
}
}
}
</script>
阅读全文