Vue monitors the ID number entered in the form to automatically fill in gender and birthday

1. First bind a v-model value to the form

<el-input type="number" v-model="form.idCard" placeholder="请输入证件号码" />

2. Use watch to monitor the input value

  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 has two parameter values, one for the new value and one for the old value

For details, see: watch monitoring in vue

Guess you like

Origin blog.csdn.net/qq_63608386/article/details/128490975