[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
法一:(复杂)
<input type="number" oninput="if(!/^[0-9]+$/.test(value))
value=value.replace(/\D/g,'');if(value>100)value=100;if(value<0)value=null"
v-model='testNum'>
核心判断:
oninput="if(!/^[0-9]+$/.test(value))
value=value.replace(/\D/g,'');if(value>100)value=100;if(value<0)value=null"
判断语句一共三个if
(1)先用正则判断是否为正整数,如果不是,就用replace方法将非数字的字符替换为为空
(2)判断范围,大于100,就让输入的值直接为100,
(3)小于0,就让输入的值为null
注意:需要将input的type类型设置为number类型,是限制只能输入数字(包含浮点型和整型数据)
法二:(简单)
<input type="number" min='0' max='100' v-model='testNum'>
直接通过设置是三个属性,type、min、max即可,就可以设置0~100的整数
注意:如果想输入浮点型数据,就将min、max 后增加小数位数定义浮点类型数据的输入
<input type="number" min='0.00' max='100.00' v-model='testNum'>
补充:
还有一个属性step,input框可以通过键盘的上下键去增加或者减少input输入的数值,step设置为多少,那么增加或者减少的幅度就是多少。例如:
<input type="number" min='0.00' max='100.00' step='0.1' v-model='testNum'>
step='0.1' 那么通过键盘加减幅度就是0.1
法二有时候会失效,没找到原因~
失效的话就用法一~