【发布时间】:2021-10-22 09:41:02
【问题描述】:
document.querySelector("#maca").addEventListener("keydown", function(e) {
if (e.target.value > this.getAttribute("max")) {
e.preventDefault();
}
})
<input type="text" name="maca" placeholder="maca" id="maca" max="7">
我试图阻止用户输入一个大于 max 属性值的数字。发生的情况是,首先允许用户写入更大的数字,然后不允许。
我该如何解决这个问题?
非常感谢!
【问题讨论】:
标签: javascript html web