我想创建输入,如果模式不匹配,我可以用空字符替换输入的字符。
模板:
<input
type="text"
:value="val"
@input="input"
/>
脚本:
import { ref } from "vue";
export default {
setup() {
let val = ref("");
const input = ({ target }) => {
val.value = target.value.replace(/[^\d]/g, "");
};
return { val, input };
},
};
最佳答案
您可以使用 watcher 删除输入的数字:
const { ref, watch } = Vue
const app = Vue.createApp({
setup() {
let val = ref("");
watch(val,
(newValue, oldValue) => {
val.value = newValue.replace(/\d+/g, "")
},
);
return { val };
},
})
app.mount('#demo')
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<div id="demo">
<div>
<input
type="text"
placeholder="Full Name"
autocomplete="off"
v-model="val"
/>
</div>
{{ val }}
</div>
关于javascript - Vue 3 输入模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73760974/