如何在jquery小数点后面自动填充0(0)?就像如果用户插入的数字不够,那么它将自动填充后面的零(0)。
示例:用户插入- 0.12345,然后它必须添加到- 0.12345000
我尝试过的:
这是我尝试过的,但我只知道如何设置点符号只能在文本字段中插入一次。
$("#Lat").on("keypress keyup blur",function (event) {
$(this).val($(this).val().replace(/[^0-9\.]/g,''));
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});
$("#Lng").on("keypress keyup blur",function (event) {
$(this).val($(this).val().replace(/[^0-9\.]/g,''));
if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
event.preventDefault();
}
});发布于 2018-08-29 07:10:23
您可以使用toFixed(n),也可以使用blur或change事件而不是keyup,因为用户在为每个输入不断更改时输入值时可能会觉得很麻烦。
$(function(){
var specialKeys = new Array();
specialKeys.push(46); // allow dot which has keyCode = 46 in specialKeys
$("#Lat,#Lng").on("blur change",function (event) {
$(this).val(parseFloat($(this).val() || 0).toFixed(8));
});
//on keypress you can restrict only number and some special characters
$("#Lat,#Lng").on("keypress", function (e) {
var keyCode = e.which ? e.which : e.keyCode;
var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
return ret;
});
// restrict copy paste
$("#Lat,#Lng").on("paste drop", function (e) {
return false;
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="Lat">
<input id="Lng">
https://stackoverflow.com/questions/52071464
复制相似问题