[英]How to prevent inserting value that is greater than to max in number field in html
I am using number field in html5,我在 html5 中使用数字字段,
here is my code,这是我的代码,
<input type="number" class="edit-items" />
i set the max of this number field using javascript.我使用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 :就像你说的,输入
number
类型的箭头可以正常工作,而不是手动更改,所以尝试使用oninput
事件来帮助您控制用户输入:
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.希望这可以帮助。
Add an event listner on the input (oninput) : check it here : on change sample在输入 (oninput) 上添加事件侦听器:在此处检查:更改示例
function maxValue(){
var numbers = document.getElementById("numbers");
console.log(numbers);
var maxQuantity = 5;
numbers.addEventListener("input", function(e) {
if(this.value>maxQuantity) {
alert("max value reached ! ");
this.value = maxQuantity;
}
})
};
window.onload = maxValue();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.