mirror of https://github.com/layui/layui
优化 input 数字输入框的精度问题
parent
b50f360c7e
commit
1ac8bba068
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue