我不知道在javascript上设置仅输入数字和%的按键 例如100%或50%
-----------------------------
折扣:| | 50%| -----------------------------
答案 0 :(得分:1)
有点难以理解,但是我想我已经解密了您的请求:D
document.getElementById('myInput').addEventListener( 'input', function() {
lastValid = this.dataset.lastValid || '';
let start = this.selectionStart + '';
let end = this.selectionEnd + '';
console.log( this.value );
console.log( this.value.match(/^[0-9]*\.?[0-9]*%?$/) );
if( this.value.match(/^[0-9]*\.?[0-9]*%?$/) !== null ) {
this.dataset.lastValid = this.value;
} else {
this.value = lastValid;
start = end -= 1;
}
this.selectionStart = start;
this.selectionEnd = end;
} );
<input type="text" id="myInput">
编辑已更新为浮动%
EDIT2 已修复:现在可以为空
EDIT3 已修复:last_valid用法