<div class="form-group row">
<label class="col-form-label col-md-3 label_required">Tax Rate:</label>
<div class="col-md-4 col-lg-8" style="position:relative">
<input id="NewTaxRate" class="form-control" formcontrolname="rate" type="text">
<span style="padding-top: 7px; font-weight: bold; position: absolute; right: 48px; top: 2px;">
%
</span>
</div>
</div>
JS代码:
$(function () {
$("#NewTaxRate").on('input',
function (e) {
var str = $(this).val();
str = str.replace(/[^\d.]/g, '');
if (str !== "") {
var index1 = str.indexOf('.');
if (str.indexOf('.') !== -1) { //带小数点
var beforePoint = str.substring(0, index1);
var afterPoint = str.substring(index1 + 1);
afterPoint = afterPoint.replace('.', '');
if (afterPoint.length > 3) {
afterPoint = afterPoint.substring(0, 3);
}
var lastStr = beforePoint + "." + afterPoint;
$(this).val(lastStr);
} else { //不带小数点
var arr = str.split('.');
var res = arr.join("").replace(/^0*/g, '');
if (res > 100) {
res = "100";
}
$(this).val(res);
}
} else {
$(this).val("");
}
});
});