【发布时间】:2021-12-13 13:40:51
【问题描述】:
我想使用 javascript 设置输入百分比
<input type="text" class="form-control" id="amount" name="amount">
我做了js,所以当我输入时,它正在添加%
var amount = document.getElementById('amount');
amount.addEventListener('input', function(e){
amount.value = amount.value.replace('%','') + '%';
})
但还是可以输入字符,我只想输入1-100,可以吗?
或者您可能对输入百分比更好的建议有什么建议
【问题讨论】:
-
尝试使用isNaN 和preventDefault 来达到您想要的结果。
-
你能给我举个例子吗?
-
附上的链接中有例子。
-
@AlghanyJagad 无论您使用哪种解决方案,请注意用户在 input 中复制粘贴值的情况。在某处提交输入值之前,您还应该涵盖这一点+验证
-
@MihaiT 感谢您的建议
标签: javascript html percentage