JS
// 金额保留两位
inputDiscount: function (e) {
let value = e.detail.value
if (value != '') {
value = value.replace(/[^\d.]/g, "")
if (/^(\d?)+(\.\d{0,2})?$/.test(value)) {} else {
value = value.substring(0, value.length - 1);
}
value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
if (value.indexOf(".") < 0 && value != "") {
value = parseFloat(value);
}
if (Number(value) > 9999) {
value = 9999
}
if (Number(value) < 1) {
value = 1
}
}
this.setData({
discount: value
})
},
html
<view class="section" id="discount" style="border-color: {{errText === 'discount' ? '#E53E06' : ''}}">
<view class="section__title">优惠金额:</view>
<input name="discount" bindinput="inputDiscount" type="digit" value="{{discount}}" placeholder-style="font-size: 30rpx;color:#AAAAAA" placeholder="请输入优惠金额" />
</view>