vue监听表单输入的身份证号自动填充性别和生日

1,先给表单绑定一个v-model值

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监听

你可能感兴趣的:(vue.js,javascript,前端)