自定义数字键盘,限制小数点后两位,根据需求调整
<template>
<view>
<view style="width: 100%;height: 500rpx;z-index: 99;">
<view class="" style="display: flex;justify-content: center;align-items: center;">
<input v-model="clockweightdata" type="number" :disabled="true"
style="width: 100rpx;height: 60rpx;border: 2rpx solid #c1c1c1;margin: 0 20rpx;border-radius: 30rpx;text-align: center;">
</view>
</view>
<view
style="width: 100%;height: 600rpx;display: flex;flex-wrap: wrap;justify-content: center;margin-top: 20rpx;">
<view class="but" v-for="item in 12" :key="item" @click="keyboard(item)">
<label v-if="item == 9 ">.</label>
<label v-if="item == 10">0</label>
<label v-if="item == 11">删除</label>
<label v-if="item < 9">{{item + 1}}</label>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
clockweightdata: ''
}
},
onReady() {
},
onLoad() {
},
methods: {
keyboard(item) {
if (item == 9 && this.clockweightdata != '') {
var x = String(this.clockweightdata).indexOf(".") + 1;
if (x != 0) {
return
}
let num = this.clockweightdata
let key = '.'
this.clockweightdata = num + key
return
}
let max = this.clockweightdata + (item + 1)
if (max >= 1000 && item < 9) {
return
}
if (item == 11 && this.clockweightdata != '') {
let num = this.clockweightdata.substring(0, this.clockweightdata.length - 1)
this.clockweightdata = num
}
var x = String(this.clockweightdata).indexOf(".") + 1;
var y = String(this.clockweightdata).length - x;
if (x != 0 && y >= 2) {
return
}
if (item < 9) {
let num = this.clockweightdata
let key = item + 1
this.clockweightdata = num + key
}
if (item == 10 && this.clockweightdata != '') {
let num = this.clockweightdata
let key = 0
this.clockweightdata = num + key
}
},
}
}
</script>
<style lang="scss" scoped>
.but {
width: 30%;
height: 150rpx;
text-align: center;
line-height: 150rpx;
border: 2rpx solid #fff;
}
</style>