我正在开发一个应用程序,我需要在其中将提交的“type=number”数量限制在某些(2 个地方)位置。以下是元素的 HTML 规则: 请有人帮我解决这个问题,在此先感谢:
<input type="number" name="amount" ng-model="orderStock.amount" class="form-control" id="amount"
step="0.01" min="10" max="9999.99" placeholder=" Please enter Amount" ng-disabled="isDisabled"
required />
最佳答案
您可以将 input 事件监听器附加到您的 input 元素,并检查它的值是否包含逗号。如果是,请检查逗号右侧是否有两个以上的字符并删除多余的字符。
function process() {
var text = document.getElementById("amount").value;
var index = text.indexOf(".");
if (index > -1) {
if (text.length - index > 3) {
document.getElementById("amount").value = text.substr(0, text.length - 1);
}
}
}
document.getElementById("amount").addEventListener("input", process);
<input type="number" name="amount" ng-model="orderStock.amount" class="form-control" id="amount" step="0.01" min="10" max="9999.99" placeholder=" Please enter Amount" ng-disabled="isDisabled" required />
如果你想把所有的代码都放在html标签里面,你可以这样做:
<input type="number" name="amount" ng-model="orderStock.amount" class="form-control" id="amount" step="0.01" min="10" max="9999.99" oninput="var text = this.value;var index = text.indexOf('.');if (index > -1) {if (text.length - index > 3) {this.value = text.substr(0, text.length - 1);}}" placeholder=" Please enter Amount" ng-disabled="isDisabled" required />
关于javascript - 如何将 "type=number"限制到某些小数位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56360977/