【问题标题】:how to prevent the character "e" from being pasted on in the input field in vue js如何防止在vue js的输入字段中粘贴字符“e”
【发布时间】:2021-11-09 20:38:02
【问题描述】:
 <vs-input
                  :value="contract.value"
                  :class="isMobileOnly ? 'w-full' : 'w-1/2'"
                  class="mx-auto"
                  type="number"
                  :id="`value-${index}`"
                  onkeydown="return event.keyCode !== 69"
                  placeholder="Value"
                  @change="changeValue(index)"
                />

onkeydown 事件可防止在输入字段中输入 e,但您仍然可以将 e 粘贴到输入字段中。如何使输入字段停止接受e,即使您粘贴了字符本身。

【问题讨论】:

  • 你需要监听paste事件,然后从输入值中剥离字符。

标签: html vue.js events input


【解决方案1】:

您可以将所有“e”字符替换为空字符串,而不是分别防止这两件事:

<template>
  <div>
    <input type="text" v-model="text" />
    <div>Value is: {{ text }}</div>
  </div>
</template>

<script>
import { ref, watchEffect } from "vue";

export default {
  setup() {
    const text = ref("");

    watchEffect(() => {
      text.value = text.value.replace(/e/, "");
    });

    return { text };
  },
};
</script>

演示:

https://codesandbox.io/s/prevent-e-character-lzi1o

【讨论】:

    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    相关资源
    最近更新 更多