ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

Vue3 + TypeScript + el-input 处理数字输入(只能输入数字,不能输入其他符号)

Vue3 + TypeScript + el-input 处理数字输入(只能输入数字,不能输入其他符号)

代码:

// 处理数字输入,只能输入数字,不能输入其他符号 const handleNumberInput = (value: string) => { // 过滤掉所有非数字字符,只保留数字 const numbersOnly = value.replace(/[^\d]/g, ""); // 转换为数字,空字符串设为0 localCapitalInfo.capitalYear = numbersOnly === "" ? 0 : parseInt(numbersOnly, 10); }; <el-input v-model="localCapitalInfo.capitalYear" clearable @input="handleNumberInput" />

效果:

返回列表