<template>
    <div style='padding: 0 250px 0 250px ;'>
        <el-form ref='ruleFormRef' :model='ruleForm' status-icon :rules='rules' label-width='120px' class='demo-ruleForm'>
            <el-form-item label='教师id' prop='tid'>
                <el-input style='width: 600px;margin: 5px 0;' v-model='ruleForm.tid' autocomplete='off' :disabled='true'/>
            </el-form-item>
            <el-form-item label='教师名' prop='tname'>
                <el-input style='width: 600px;margin: 5px 0;' v-model='ruleForm.tname' autocomplete='off' :disabled='false' />
            </el-form-item>
            <el-form-item label='院名' prop='departmentName'>
                <el-input style='width: 600px;margin: 5px 0;' v-model='ruleForm.departmentName' autocomplete='off' :disabled='true' />
            </el-form-item>
            <el-form-item label='职位' prop='tsId'>
                <el-input style='width: 600px;margin: 5px 0;' v-model='ruleForm.tsId' autocomplete='off' :disabled='true' />
            </el-form-item>
            <el-form-item label='年龄' prop='age'>
                <el-input style='width: 600px;margin: 5px 0;' v-model.number='ruleForm.age' :disabled='false' />
            </el-form-item>
            <el-form-item label='性别' prop='sex'>
                <el-radio-group style='margin: 0 auto;' v-model='ruleForm.sex' :disabled='false'>
                    <el-radio label='男' >男</el-radio>
                    <el-radio label='女' >女</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label='电话' prop='phone'>
                <el-input style='width: 600px;margin: 5px 0;' v-model='ruleForm.phone' autocomplete='off' />
            </el-form-item>
            <el-form-item label='密码' prop='tpassword'>
                <el-input style='width: 600px;margin: 5px 0;' v-model='ruleForm.tpassword' autocomplete='off' />
            </el-form-item>
            <el-form-item>
                <el-button style='margin: 0 auto;' type='primary' @click='submitForm(ruleFormRef)'>修改</el-button>
                <el-button style='margin: 0 auto;' type='primary' @click='resetForm(ruleFormRef)'>Reset</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script lang='ts' setup>
import { reactive, ref } from 'vue'
import axios from 'axios';
import type { FormInstance, FormRules } from 'element-plus'

const ruleFormRef = ref<FormInstance>()

const checkAge = (_rule: any, value: any, callback: any) => {
    if (!value) {
        return callback(new Error('Please input the age'))
    }
    setTimeout(() => {
        if (!Number.isInteger(value)) {
            callback(new Error('Please input digits'))
        } else {
            if (value < 18) {
                callback(new Error('Age must be greater than 18'))
            } else {
                callback()
            }
        }
    }, 1000)
}


const ruleForm = ref({
    tid: '',
    tname: '',
    departmentName: '',
    tsId: '',
    phone: '',
    age: '',
    sex: '',
    tpassword:''
})
axios({
    method: 'Get',
    url: '/api/myInformationServlet',
    params: { tid: localStorage.getItem('tid') }
}).then(function (result) {
    console.log(result);
    ruleForm.value = result.data.data;
    console.log(ruleForm.value);
})

const rules = reactive<FormRules<typeof ruleForm>>({
    age: [{ validator: checkAge, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            axios({
                method: 'POST',
                url: '/api/myInformation',
                data: ruleForm.value
            }).then(function (result) {
                if(result.data.flag){
                    alert('修改成功')
                }else{
                    alert(result.data.message)
                }
            })
        } else {
            console.log('error submit!')
            return false
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}
</script> 
<p>点击修改传入的参数tname和tpassword为空怎么解决</p>
<p>根据提供的代码,<code>tname</code>和<code>tpassword</code>为空的原因可能是在获取数据的axios请求中,没有正确设置<code>ruleForm</code>的值。请确保<code>result.data.data</code>中包含<code>tid</code>、<code>tname</code>和<code>tpassword</code>的属性,并且这些属性的值正确地赋给了<code>ruleForm</code>。</p>
<p>另外,可以在获取到数据后,使用<code>console.log()</code>来确认<code>ruleForm</code>的值是否正确地被赋值。例如:</p>
<pre><code class="language-javascript">axios({
    method: 'Get',
    url: '/api/myInformationServlet',
    params: { tid: localStorage.getItem('tid') }
}).then(function (result) {
    console.log(result);
    ruleForm.value = result.data.data;
    console.log(ruleForm.value);
})
</code></pre>
<p>如果在控制台输出中,<code>ruleForm</code>的值正确地包含了<code>tid</code>、<code>tname</code>和<code>tpassword</code>的值,那么问题可能出现在其他地方。请进一步检查代码逻辑。</p>
修改教师信息 - 教师管理系统

原文地址: https://www.cveoy.top/t/topic/qxRo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录