当前位置: 首页 > news >正文

uniapp中输入金额的过滤(只允许输入数字和小数点)

一、完整代码:

<template><view class="numberIndex" :style="{ paddingTop: navbarHeight + 'px' }"><view class="custom-navbar" :style="{ paddingTop: statusBarHeight + 'px' }"><view class="navbar-left" @click="goBack"><view class="nav-arrow"></view></view><view class="navbar-title">{{title}}</view></view><view class="title">选择固定数字,或者自行输入</view><view class="input"><input class="uni-input" type="text" confirm-type="done" placeholder="请输入或选择" :value="inputValue"@blur="handleBlur" @input="changeInput"placeholder-style='font-weight: 400!important;font-size: 36rpx!important;color: #7D8EB3!important;' /><view class="inputClean" v-show="showClearIcon" @click="clearIcon"><image src="https://ele.qre.com.cn/charging/clean.png" mode="aspectFit" class="input-icon" /></view></view><view class="list"><view v-for="(item,index) in list" :key="index" @click="chooseNumber(item)":class="(!!chooseValue && (chooseValue === item)) ? 'list-box highLight' : 'list-box'">{{item}}</view></view></view>
</template><script>export default {data() {return {title: '数字输入',navbarHeight: 0, // 导航栏高度statusBarHeight: 0,inputValue: '', // 输入的数字showClearIcon: false,list: [20, 30, 40, 50, 60, 70],chooseValue: 20, // 选择的数字minVal: 5, // 输入的最小数字maxVal: 1000 // 输入的最大数字}},onLoad() {// 获取系统信息计算导航栏高度const systemInfo = uni.getSystemInfoSync();this.statusBarHeight = systemInfo.statusBarHeight;this.navbarHeight = this.statusBarHeight + 44; // 44是导航栏标准高度this.inputValue = '20'},methods: {goBack() {// 获取当前页面栈const pages = getCurrentPages();if (pages.length > 1) {// 如果有上一页,则返回uni.navigateBack();} else {// 如果是首页,则跳转到指定页面uni.redirectTo({url: '/pages/index/index' // 替换为您的首页路径});}},changeInput: function(e) {let value = e.detail.value.trim()value = value.replace(/[^\d.]/g, '');const dotArr = value.split('.');if (dotArr.length > 2) {value = dotArr[0] + '.' + dotArr.slice(1).join('');}// 2. 处理“过滤后的值与原输入值一致”的场景if (value === this.inputValue) {// 临时修改 inputValue 为其他值,再立即改回,强制触发视图更新this.inputValue = '';this.$nextTick(() => {this.inputValue = value;});} else {// 普通场景:直接赋值this.inputValue = value;}if (value) {this.showClearIcon = true;this.chooseValue = 0} else {this.clearIcon()}},// 失焦时强制修正handleBlur(e) {if (!this.inputValue) {this.inputValue = '20'return}const numValue = Number(this.inputValue)if (numValue < this.minVal) {this.inputValue = this.minVal.toString()} else if (numValue > this.maxVal) {this.inputValue = this.maxVal.toString()}},clearIcon() {this.inputValue = '20';this.chooseValue = 20this.showClearIcon = false;},chooseNumber(item) {this.inputValue = itemthis.chooseValue = item}}}
</script><style scoped>.numberIndex {width: 100vw;height: 100vh;background-color: #fff;overflow: hidden;}.numberIndex .custom-navbar {position: fixed;top: 0;left: 0;right: 0;z-index: 999;display: flex;align-items: center;background-color: #ffffff;}.numberIndex .nav-arrow {width: 18rpx;height: 18rpx;border-left: 2rpx solid #010101;border-bottom: 2rpx solid #010101;transform: rotate(45deg);margin-left: 46rpx;}.numberIndex .navbar-title {flex: 1;height: 88rpx;line-height: 88rpx;text-align: center;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;font-weight: 600;font-size: 32rpx;color: #000000;}.numberIndex .title {padding: 20rpx 30rpx;font-weight: 600;font-size: 36rpx;color: #0A1833;}.numberIndex .input {box-sizing: border-box;display: flex;flex-wrap: nowrap;align-items: center;height: 112rpx;border-bottom: 2rpx solid #F4F7FB;margin: 0 30rpx;}.numberIndex .input-uint {margin-right: 8rpx;font-weight: 400;font-size: 36rpx;color: #0A1833;}.numberIndex .uni-input {width: calc(100vw - 104rpx);font-weight: 600;font-size: 36rpx;color: #0A1833;}.numberIndex .input-icon {width: 32rpx;height: 32rpx;}.numberIndex .inputClean {z-index: 300;position: relative;padding: 10rpx;}.numberIndex .list {display: grid;grid-template-columns: repeat(3, 1fr);gap: 30rpx;margin: 20rpx 30rpx;}.numberIndex .list-box {display: flex;justify-content: center;align-items: center;background: #E9F0FF;border: 2rpx solid #E9F0FF;color: #7D8EB3;border-radius: 8rpx;padding: 18rpx 0;font-weight: 600;font-size: 32rpx;}.numberIndex .highLight {background: #FFFFFF;border: 2rpx solid #3377FF;color: #3377FF;}
</style>

二、效果预览:

在这里插入图片描述

http://www.dtcms.com/a/362831.html

相关文章:

  • Redis分层缓存
  • kukekey在线搭建k8sV1.30.4版本
  • VMWare ubuntu24.04安装(安装ubuntu安装)
  • InnoDB存储引擎-逻辑存储结构
  • Qwen3-30B-A3B 模型解析
  • 【LeetCode牛客数据结构】单链表的应用
  • C语言(长期更新)第12讲:指针二详解
  • 【嵌入式电机控制#进阶6】三段启动法
  • 怎么为服务器设置或重置服务器密码?
  • 【Vue2 ✨】Vue2 入门之旅(九):Vue Router 入门
  • JetBrains 2025 全家桶 11合1 Windows直装(含 IDEA PyCharm、WebStorm、DataSpell、DataGrip等
  • [密码学实战]智能密码钥匙SKF库软实现(四十六)
  • LabVIEW应急柴油发电机组诊断装置
  • LabVIEW振动信号积分处理
  • 【设计模式】通俗讲解设计模式的七大原则
  • 【设计模式】从游戏角度开始了解设计模式 --- 创建型模式(一)
  • Python OpenCV图像处理与深度学习:Python OpenCV性能优化与高效图像处理
  • VGG改进(7):基于Spatial Attention的性能优化
  • 从“叠加”到“重叠”:Overlay 与 Overlap 双引擎驱动技术性能优化
  • Spring MVC + JSP 项目的配置流程,适合传统 Java Web 项目开发
  • 【MySQL】初识数据库基础
  • RAG-检索进阶
  • 【一张图看懂Kafka消息队列架构】
  • 【C++】编写通用模板代码的重要技巧:T()
  • 与后端对话:在React中优雅地请求API数据 (Fetch/Axios)
  • 基于STM32的智能语音浴缸设计
  • 工业视觉光源选色指南:白光通用、蓝光显瑕疵、红光能穿透,看完直接用
  • 推荐一个论文阅读工具ivySCI
  • C++内存管理,模板初阶(泛型编程)
  • 项目组文档标准不一致的原因有哪些