表单代码示例

<template>
  <el-form ref="form" :model="formData" :rules="formRules" label-width="100px">
    <el-form-item label="姓名" prop="name">
      <el-input v-model="formData.name"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="formData.phone"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model.number="formData.age"></el-input>
    </el-form-item>
    <el-form-item label="学校" prop="school">
      <el-input v-model="formData.school"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="gender">
      <el-radio-group v-model="formData.gender">
        <el-radio label="male"></el-radio>
        <el-radio label="female"></el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="地点" prop="location">
      <el-radio-group v-model="locationType">
        <el-radio label="province">选择省市区</el-radio>
        <el-radio label="custom">手填地点</el-radio>
      </el-radio-group>
      <el-collapse v-model="collapseVisible" v-if="locationType === 'province'">
        <!-- 省市区组件,这里使用Element Plus的级联选择器 -->
        <el-cascader
          v-model="formData.location"
          :options="options"
          placeholder="请选择省市区"
          clearable
          :props="{ value: 'code', label: 'name', children: 'children' }"
        ></el-cascader>
      </el-collapse>
      <el-input v-model="formData.location" v-else placeholder="请输入手填地点"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { ref } from 'vue';
import axios from 'axios';
import { ElForm, ElFormItem, ElInput, ElRadioGroup, ElRadio, ElButton, ElCollapse, ElCascader } from 'element-plus';

export default {
  components: {
    ElForm,
    ElFormItem,
    ElInput,
    ElRadioGroup,
    ElRadio,
    ElButton,
    ElCollapse,
    ElCascader,
  },
  data() {
    return {
      formData: {
        name: '',
        phone: '',
        age: '',
        school: '',
        gender: '',
        location: '',
      },
      formRules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^[1][3,4,5,7,8][0-9]{9}$/, message: '手机号格式不正确', trigger: 'blur' },
        ],
        age: [{ required: true, message: '请输入年龄', trigger: 'blur' }],
        school: [{ required: true, message: '请输入学校', trigger: 'blur' }],
        gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
        location: [{ required: true, message: '请选择或输入地点', trigger: 'blur' }],
      },
      locationType: 'province', // 默认选择省市区
      collapseVisible: false,
      options: [
        // 省市区数据,根据实际情况填写
        { code: '110000', name: '北京市', children: [...] },
        { code: '120000', name: '天津市', children: [...] },
        // 其他省市区数据
      ],
    };
  },
  methods: {
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 表单验证通过,可以提交数据
          axios.post('/api/submit', this.formData)
            .then(response => {
              console.log('提交成功', response);
              // 根据实际需求处理提交成功后的逻辑
            })
            .catch(error => {
              console.error('提交失败', error);
              // 根据实际需求处理提交失败后的逻辑
            });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style>
/* 可选:自定义样式 */
</style>

关注微信公众号温暖前端,不定期分享前端知识点和前端资料↓↓↓

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温暖前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值