vue.js - VUE.JS + Bootstrap - How to capitalize first letter in the same input
问题描述
I would like get in input capitalize first letter when I'm starting write.
<b-form-input v-model="formSurname" id="Surname" name="Surname" placeholder="Surname" type="text" maxlength="50" required> </b-form-input>
I have also filter for this. But v-model="formSurname | capitalize"
doesn't work I tried :value="formSurname | capitalize"
but it doesn't work too
Vue.filter("capitalize", function (value) {
if (!value)
return '';
value = value.toString();
return value.charAt(0).toUpperCase() + value.slice(1);
});
How to make an input to change my first letter in real time?
解决方案
If you need to store the value with the uppercase, use a computed property with set and get methods :
computed: {
formSurnameCapital: {
get: function () {
return this.formSurname;
},
// setter
set: function (newSurname) {
if(newSurname.length < 1) {this.formSurname = ''; return}
this.formSurname = newSurname.replace(/^./, newSurname[0].toUpperCase());
}
}
}
And in your template :
<b-form-input v-model="formSurnameCapital" id="Surname" name="Surname" placeholder="Surname" type="text" maxlength="50" required> </b-form-input>
Else, if is just about displaying it uppercase, you can simply use css :
.toCapitalFirst {
text-transform: capitalize;
}
推荐阅读
- css - 为什么我们不能使用 css 在 bootstrap 4 中更改徽章类的颜色?
- reactjs - 反应 setState 回调不显示当前状态
- spring-boot - 使用 @Autowired 和 @PersistenceContext 注释 EntityManager 有什么区别?应该首选哪一个?
- python - 如何扩大限制以包括 48 小时内的最长工作时间?
- python - 为什么我不能在函数内创建类对象?
- powershell - 将文件共享路径作为参数读取时出错
- javascript - 无法读取未定义的属性,并且此指向不在正确的位置
- python-3.x - 具有色阶和一种颜色的不需要值的颜色图
- python - 在 python 中实现 REP-tree 和 M5P-tree 算法
- ruby - Can't install gems using asdf