<template>
<div>
<input v-model="inputValue" @input="convertToHalfWidth" />
</div>
</template>
<script>
export default {
data() {
return {
inputValue: ''
};
},
methods: {
convertToHalfWidth(event) {
const { target } = event;
const value = target.value;
const halfWidthValue = value.replace(/[\uff01-\uff5e]/g, (match) => {
return String.fromCharCode(match.charCodeAt(0) - 0xfee0);
});
this.inputValue = halfWidthValue;
// 更新视图,防止ngModel失效
this.$nextTick(() => {
target.value = halfWidthValue;
});
}
}
};
</script>
vue 监听input全角转半角
最新推荐文章于 2024-07-22 15:13:59 发布