Skip to content

InputNumber 数字输入框

通过鼠标或键盘,输入范围内的数值。

代码演示

基础用法

数字输入框。

当前值:3
vue
<template>
  <div>
    <a-input-number id="inputNumber" v-model:value="value" :min="1" :max="10" />
    当前值:{{ value }}
  </div>
</template>
<script setup>
import { ref } from 'vue';
const value = ref(3);
</script>

不可用

点击按钮切换可用状态。

vue
<template>
  <div>
    <a-input-number v-model:value="value" :min="1" :max="10" :disabled="disabled" />
    <div style="margin-top: 20px">
      <a-button type="primary" @click="toggle">Toggle disabled</a-button>
    </div>
  </div>
</template>
<script setup>
import { ref } from 'vue';
const value = ref(3);
const disabled = ref(true);
const toggle = () => {
  disabled.value = !disabled.value;
};
</script>

高精度小数

通过 stringMode 开启高精度小数支持,change 事件将返回 string 类型。 对于旧版浏览器,你需要 BigInt polyfill。

vue
<template>
  <a-input-number
    v-model:value="value2"
    style="width: 200px"
    :min="0"
    :max="10"
    :step="0.00000000000001"
    string-mode
  />
</template>
<script setup>
import { ref } from 'vue';
const value = ref('1');
</script>

格式化展示

通过 formatter 格式化数字,以展示具有具体含义的数据,往往需要配合 parser 一起使用。

vue
<template>
  <a-space>
    <a-input-number
      v-model:value="value1"
      :formatter="value => `$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')"
      :parser="value => value.replace(/\$\s?|(,*)/g, '')"
    />
    <a-input-number
      v-model:value="value2"
      :min="0"
      :max="100"
      :formatter="value => `${value}%`"
      :parser="value => value.replace('%', '')"
    />
  </a-space>
</template>
<script setup>
import { ref } from 'vue';
const value1 = ref(1000);
const value2 = ref(100);
</script>

键盘行为

使用 keyboard 属性可以控制键盘行为。

vue
<template>
  <a-space>
    <a-input-number v-model:value="value" :keyboard="keyboard" :min="1" :max="10" />
    <a-checkbox v-model:checked="keyboard">Toggle keyboard</a-checkbox>
  </a-space>
</template>
<script setup>
import { ref } from 'vue';
const value = ref(3);
const keyboard = ref(true);
</script>

超出边界

当通过受控将 value 超出边界时,提供警告样式。

vue
<template>
  <a-space>
    <a-input-number v-model:value="value" :min="1" :max="10" />
    <a-button type="primary" @click="value = 99">Reset</a-button>
  </a-space>
</template>
<script setup>
import { ref } from 'vue';
const value = ref(99);
</script>

自定义状态

使用 status 为 InputNumber 添加状态,可选 error 或者 warning

vue
<template>
  <a-space direction="vertical" style="width: 100%">
    <a-input-number status="error" style="width: 100%" />
    <a-input-number status="warning" style="width: 100%" />
  </a-space>
</template>
<script setup>
export {};
</script>

API

属性如下

成员说明类型默认值
addonAfter带标签的 input,设置后置标签slot-
addonBefore带标签的 input,设置前置标签slot-
autofocus自动获取焦点booleanfalse
bordered是否有边框booleantrue
controls是否显示增减按钮booleantrue
decimalSeparator小数点string-
defaultValue初始值number
disabled禁用booleanfalse
formatter指定输入框展示值的格式function(value: number | string, info: { userTyping: boolean, input: string }): string-
keyboard是否启用键盘快捷行为booleantrue
max最大值numberInfinity
min最小值number-Infinity
parser指定从 formatter 里转换回数字的方式,和 formatter 搭配使用function( string): number-
precision数值精度number-
prefix带有前缀图标的 inputslot-
size输入框大小string-
status设置校验状态'error' | 'warning'-
step每次改变步数,可以为小数number | string1
stringMode字符值模式,开启后支持高精度小数。同时 change 事件将返回 string 类型booleanfalse
upIcon自定义上箭头图标slot<UpOutlined />
downIcon自定义下箭头图标slot<DownOutlined />
value(v-model)当前值number

事件

事件名称说明回调参数
change变化回调Function(value: number | string)
pressEnter按下回车的回调function(e)
step点击上下箭头的回调(value: number, info: { offset: number, type: 'up'

方法

名称描述
blur()移除焦点
focus()获取焦点