优化 input 数字输入框的精度问题

pull/1307/head
贤心 2023-07-11 16:09:59 +08:00
parent b50f360c7e
commit 1ac8bba068
1 changed files with 10 additions and 5 deletions

View File

@ -277,10 +277,11 @@ layui.define(['lay', 'layer', 'util'], function(exports){
value: ['up', 'down'],
split: true,
className: 'layui-input-number',
disabled: disabled, // 跟随输入框禁用状态
disabled: othis.is('[disabled]'), // 跟随输入框禁用状态
click: function(elem){
var index = $(this).index();
var value = elem.val();
var rawValue = value;
var step = Number(elem.attr('step')) || 1; // 加减的数字间隔
var min = Number(elem.attr('min'));
var max = Number(elem.attr('max'));
@ -294,11 +295,15 @@ layui.define(['lay', 'layer', 'util'], function(exports){
if(value < min) value = min;
if(value > max) value = max;
// 小数点后保留位数
var fixed = function(step){
var decimals = (step.match(/\.(\d+$)/) || [])[1] || '';
// 获取小数点后位数
var decimals = function(step){
var decimals = (step.toString().match(/\.(\d+$)/) || [])[1] || '';
return decimals.length;
}(step.toString());
};
// 位数比较
var fixed = Math.max(decimals(step), decimals(rawValue));
if(fixed) value = value.toFixed(fixed);
elem.val(value);