javascript - Vue 3 输入模式

标签 javascript vue.js vuejs3

我想创建输入,如果模式不匹配,我可以用空字符替换输入的字符。

模板:

<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 };
  },
};

Sandbox

最佳答案

您可以使用 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/

相关文章:

html - vue 中的 <component> 总是渲染换行符

vue.js - 在 vue-component 中使用 vue-html-editor

javascript - 如何创建叔叔节点(=父节点的兄弟)

JavaScript/Node.js : function returning another function based on parameters

javascript - 展开网页上的所有可折叠部分

vue.js - 如何在 Nuxt 3 中启用reactivityTransform?

vue.js - 使用 SCSS 和 Vue 3 避免 v-deep 重复

javascript - 服务器端事件推送通知

vue.js - vuejs 每周日历不显示事件

laravel - Vue 3,Uncaught TypeError : Vue. use is not a function