【解决】微信小程序、uni-app input 关于设置type=“digit“类型时可以输入多个小数点.问题、只允许数据整数或者小数类型

有一个设置价格的场景,只允许数据整数或者小数,用的uni-app中的input组件,解决了原生组件【可以输入多个小数点“.”】的问题,博主这里只是用于微信小程序端,安卓和ios系统均已验证通过,不确定其他端是否有异常。话不多说,代码如下,欢迎各路高手指正。

<template>
  <view>
    <!-- 价格 -->
    <input
      auto-blur
      style="width: 100%; text-align: right; color: red"
      type="digit"
      v-model="price"
      placeholder-styl,e="color: red;"
      placeholder="¥ 0.00"
      :maxlength="9"
      @input="handlePrice"
    />
  </view>
</template>

<script>
export default {
  data() {
    return {
      price: "",
    };
  },
  methods: {
    handlePrice() {
      const { price } = this;
      const dotIndex = price.indexOf(".");
      const dotLastIndex = price.lastIndexOf(".");
      if (dotIndex !== dotLastIndex) {
        const p = price.slice(0, dotLastIndex);
        this.$nextTick(() => {
          this.price = p;
        });
      }
    },
  },
};
</script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值