Element Plus 中 el-input 限制为数值输入的方法

方法一:使用 type="number"

javascript 复制代码
<template>
  <el-input v-model="numberValue" type="number" />
</template>

<script setup>
import { ref } from 'vue';

const numberValue = ref('');
</script>

方法二:使用自定义指令过滤非数字

javascript 复制代码
<template>
  <el-input 
    v-model="numberValue" 
    v-number-only
    placeholder="只能输入数字"
  />
</template>

<script setup>
import { ref } from 'vue';

const numberValue = ref('');

// 自定义指令
const vNumberOnly = {
  mounted(el) {
    el.querySelector('input').addEventListener('input', (e) => {
      e.target.value = e.target.value.replace(/[^\d]/g, '');
    });
  }
};
</script>

方法三:使用 onInput 事件处理

javascript 复制代码
<template>
  <el-input 
    v-model="numberValue" 
    @input="handleNumberInput"
    placeholder="只能输入数字"
  />
</template>

<script setup>
import { ref } from 'vue';

const numberValue = ref('');

const handleNumberInput = (value) => {
  numberValue.value = value.replace(/[^\d]/g, '');
};
</script>
相关推荐
一条上岸小咸鱼12 分钟前
Kotlin 基本数据类型(一):Numbers
android·前端·kotlin
前端小巷子36 分钟前
Vue 事件绑定机制
前端·vue.js·面试
uhakadotcom44 分钟前
开源:subdomainpy快速高效的 Python 子域名检测工具
前端·后端·面试
爱加班的猫1 小时前
Node.js 中 require 函数的原理深度解析
前端·node.js
用户8165111263971 小时前
WWDC 2025 Build a SwiftUI app with the new design
前端
伍哥的传说1 小时前
Vue 3.5重磅更新:响应式Props解构,让组件开发更简洁高效
前端·javascript·vue.js·defineprops·vue 3.5·响应式props解构·vue.js新特性
阅文作家助手开发团队_山神1 小时前
第一章: Mac Flutter Engine开发准备工作
前端·flutter
菜牙买菜1 小时前
Hicharts入门
前端·vue.js·数据可视化
摸着石头过河的石头1 小时前
手把手教你入门 MCP:模型上下文协议与 Trae IDE 中的实践
前端·mcp