<template>
  <div>
    <input type="text" v-model="inputValue" @input="filterInput" />
  </div>
</template>
<script>
export default {
  data() {
    return {
      inputValue: ''
    };
  },
  methods: {
    filterInput() {
      // 使用正则表达式过滤非数字字符
      this.inputValue = this.inputValue.replace(/\D/g, '');
    }
  }
};
</script>
<p>在上面的示例中,我们使用了v-model指令来绑定输入框的值到<code>inputValue</code>属性上。然后,在<code>filterInput</code>方法中,我们使用正则表达式<code>\/D\/g</code>来过滤非数字字符。每当用户输入或修改输入框的内容时,<code>filterInput</code>方法就会被调用,确保只有数字字符被保留在<code>inputValue</code>中。</p>
<p>这样,输入框就只允许输入数字了。</p>

原文地址: https://www.cveoy.top/t/topic/pNcA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录