1、form表单 <el-form-item label="比率" prop="ratio"> <el-input v-model="form.ratio" placeholder="请输入比率" @change="changeRatio"/> <span style="color: red; font-size: 10px" v-if="spanFlag">请输入整数,且不能大于100</span> </el-form-item> 2、data() { return { spanFlag: false, } } 3、rules: { ratio: [ { required: true, message: "请输入整数,且不能大于100", trigger: "blur", }, ], }, 4、created() { if (this.actionEdit === "update") { this.form.ratio = this.form.ratio + "%"; } }, 5、methods: { changeRatio() { var regu =/^([0-9]{1,2}|100)$/; var re = new RegExp(regu); debugger if (re.test(this.form.ratio)) { this.spanFlag = false; let s = this.form.ratio; this.form.ratio = s + "%"; } else { this.spanFlag = true; } }, }