diff --git a/components/zzjc-num-keyboard/zzjc-num-keyboard.vue b/components/zzjc-num-keyboard/zzjc-num-keyboard.vue index ae728d7..ca22a3d 100644 --- a/components/zzjc-num-keyboard/zzjc-num-keyboard.vue +++ b/components/zzjc-num-keyboard/zzjc-num-keyboard.vue @@ -81,8 +81,22 @@ const jianshao = () => { } const changeNums = (item, index) => { - if (item.text === '.') { - if (nums.value.indexOf('.') !== -1 || nums.value.length === 0) { + const inputChar = item.text + // 处理负号 '-' + if (inputChar === '-') { + // 只允许在输入为空时输入负号(即只能作为第一个字符) + if (nums.value === '') { + nums.value = '-' + } + return // 无论是否成功,都不继续处理 + } + if (inputChar === '.') { + // 不能以 '.' 开头(除非已有负号) + if (nums.value === '' || nums.value === '-') { + return false + } + // 已存在小数点则不允许再输入 + if (nums.value.indexOf('.') !== -1) { return false } } @@ -96,7 +110,7 @@ const changeNums = (item, index) => { } } - nums.value += item.text + nums.value += inputChar } const getListItemStyle = index => {