1,先给表单绑定一个v-model值
<el-input type="number" v-model="form.idCard" placeholder="请输入证件号码" />
2,使用watch监听输入的值
watch(form, (newName, oldName) => { var numid = newName.idCard.split(''); if (numid.length == 18) { var sex = newName.idCard.substring(16, 17); form.sex = sex % 2 == 1 ? '男' : '女'; var bir = newName.idCard.substr(6, 8); form.birthday = bir.replace(/(.{4})(.{2})/, '$1-$2-'); } else { form.sex = ''; form.birthday = ''; } });
watch有两个参数值,一个为新值,一个为旧值
详情可见:vue中的watch监听