mirror of https://github.com/layui/layui
fix
parent
808d8aacc7
commit
4d223e41df
|
@ -189,7 +189,6 @@ layui.define(['lay', 'layer', 'util'], function(exports){
|
||||||
var isInit = eventType === 'init';
|
var isInit = eventType === 'init';
|
||||||
var isBadInput = isNaN(value);
|
var isBadInput = isNaN(value);
|
||||||
|
|
||||||
|
|
||||||
elem.toggleClass(BAD_INPUT, isBadInput);
|
elem.toggleClass(BAD_INPUT, isBadInput);
|
||||||
if(isBadInput) return; // 若非数字,则不作处理
|
if(isBadInput) return; // 若非数字,则不作处理
|
||||||
|
|
||||||
|
@ -220,6 +219,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){
|
||||||
value = value.toFixed(precision);
|
value = value.toFixed(precision);
|
||||||
}
|
}
|
||||||
elem.val(value);
|
elem.val(value);
|
||||||
|
elem.attr('lay-input-mirror', elem.val())
|
||||||
}
|
}
|
||||||
|
|
||||||
// 超出范围的样式
|
// 超出范围的样式
|
||||||
|
@ -367,17 +367,36 @@ layui.define(['lay', 'layer', 'util'], function(exports){
|
||||||
init: function(elem){
|
init: function(elem){
|
||||||
// 旧版浏览器不支持更改 input 元素的 type 属性,需要主动设置 text
|
// 旧版浏览器不支持更改 input 元素的 type 属性,需要主动设置 text
|
||||||
if(elem.attr('type') === 'text'){
|
if(elem.attr('type') === 'text'){
|
||||||
var oldValue = isNaN(Number(elem.val())) ? '' : elem.val();
|
var ns = '.lay_input_number';
|
||||||
elem.off('.lay_input_number')
|
var skipCheck = false;
|
||||||
.on('input.lay_input_number propertychange.lay_input_number', function(e){
|
var isComposition = false;
|
||||||
if(e.type === 'propertychange' && e.originalEvent.propertyName !== 'value') return;
|
// 旧版浏览器不支持 beforeInput 事件,需要设置一个 attr 存储输入前的值
|
||||||
if(canInputNumber(this.value)){
|
elem.attr('lay-input-mirror', elem.val());
|
||||||
oldValue = this.value;
|
elem.off(ns);
|
||||||
}else{
|
// 旧版浏览器不支持 event.inputType 属性,需要用 keydown 事件来判断是否跳过输入检查
|
||||||
this.value = oldValue;
|
elem.on('keydown' + ns, function (e) {
|
||||||
}
|
skipCheck = false;
|
||||||
elem.toggleClass(BAD_INPUT, isNaN(Number(this.value)));
|
if (e.keyCode === 8 || e.keyCode === 46) { // Backspace || Delete
|
||||||
});
|
skipCheck = true;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
elem.on('input' + ns + ' propertychange' + ns, function (e) {
|
||||||
|
if (isComposition || (e.type === 'propertychange' && e.originalEvent.propertyName !== 'value')) return;
|
||||||
|
if (skipCheck || canInputNumber(this.value)) {
|
||||||
|
elem.attr('lay-input-mirror', this.value);
|
||||||
|
} else {
|
||||||
|
// 恢复输入前的值
|
||||||
|
this.value = elem.attr('lay-input-mirror');
|
||||||
|
}
|
||||||
|
elem.toggleClass(BAD_INPUT, isNaN(Number(this.value)));
|
||||||
|
});
|
||||||
|
elem.on('compositionstart' + ns, function () {
|
||||||
|
isComposition = true;
|
||||||
|
});
|
||||||
|
elem.on('compositionend' + ns, function () {
|
||||||
|
isComposition = false;
|
||||||
|
elem.trigger('input');
|
||||||
|
})
|
||||||
}
|
}
|
||||||
handleInputNumber.call(this, elem, 'init')
|
handleInputNumber.call(this, elem, 'init')
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue