Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在考虑强迫人们只能写 0-100 之间的数字(因为通过百分比获得的最大数字是 100%)。
有没有办法强制人们输入 0-100% 之间的数字?我已经使用 javascript 将它们限制为 3 位数。
我正在寻找为此使用javascript。
您可以使用数字输入 <input type="number" min="0" max="100">,但它不适用于所有浏览器,因此您必须通过一些 javascript 对其进行补充,以便仔细检查该值是否正确。
<input type="number" min="0" max="100">
您可以使用显示为滑块的 Range 类型输入元素。
<input type="range" name="percentage" min="0" max="100">
或者,您可以在 onchange 或 onkeyup 事件上运行验证函数,检查值是否大于 100。
<input type="text" name="percentage" onkeyup="validate(this);"> function validate(el){ if(el.value > 100){ el.value = 100; } }