How to prevent inserting value that is greater than to max in number field in html(如何防止在html中的数字字段中插入大于最大值的值)
问题描述
我在html5中使用数字字段,
这是我的代码,
我使用 javascript 设置了这个数字字段的最大值.
element.max = 数量;元素.min = 0;element.step = 1;
如果我在数字字段中使用向上箭头.大于最大值的值是不可能的.但是当我尝试在该字段中插入时,可能会出现大于最大值的值.
我想阻止它.怎么样?
就像你说的 number
类型的输入可以很好地使用箭头而不是手动更改,所以尝试使用 oninput
事件来帮助您控制用户输入:
document.getElementsByClassName('edit-items')[0].oninput = function () {var max = parseInt(this.max);if (parseInt(this.value) > max) {this.value = 最大值;}}
<input type="number" class="edit-items" max='10'/>
上一页>
希望这会有所帮助.
I am using number field in html5,
here is my code,
<input type="number" class="edit-items" />
i set the max of this number field using javascript.
element.max = quantity;
element.min = 0;
element.step = 1;
If i am using the arrow-up in the number field. Greater value than the max is not possible. But when I tried to insert in the field, greater value than the max is possible.
I would like to prevent it. How?
Like you said input of type number
will work fine with arrows and not with manual change so try to use oninput
event to help you control user inputs :
document.getElementsByClassName('edit-items')[0].oninput = function () {
var max = parseInt(this.max);
if (parseInt(this.value) > max) {
this.value = max;
}
}
<input type="number" class="edit-items" max='10'/>
Hope this helps.
这篇关于如何防止在html中的数字字段中插入大于最大值的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何防止在html中的数字字段中插入大于最大值的值


- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Flexslider 箭头未正确显示 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01