這很難做到。缺乏努力(共享代碼努力)使我無法做到這一點。但複雜性使我分享它。也可能會爲許多其他人提供幫助。
Working Demo
document.getElementById("demo").onkeydown = function (e) {
//console.log(e.keyCode);
if (e.keyCode != 37 && e.keyCode != 39 && e.keyCode != 8 && e.keyCode != 46)
e.preventDefault();
if (e.keyCode == 8 || e.keyCode == 46) {
//If already key down (backspaceOrDel=1) then no affect
if (backspaceOrDel == 1)
e.preventDefault();
else
backspaceOrDel = 1;
return;
}
if (e.keyCode < 48 || (e.keyCode > 57 && e.keyCode <96) || e.keyCode > 105)
return;
try {
var val = this.value;
var val1 = 0;
if (val == 0) {
val1 = e.key/100;
}
else {
//very tricky. We needed val1=val*10+e.key but it does not
//work correctly with floats in javascript.
//Here you have to different than android in logic
var val1 = parseFloat(val) * 1000;
val1 += parseFloat(e.key);
val1 = val1/100;
}
val1 = val1.toFixed(2);
if (!isNaN(val1))
this.value = val1;
}
catch (ex) {
alert("Invalid Amount");
}
};
你必須先試一下。 –
我已經嘗試了角度解決方案,但我不確定如何實現它。我在iOS項目文本字段中實現了類似的功能,但是,我不確定邏輯是否相同 –
只是回答相同:http://stackoverflow.com/questions/38972448/force-input-number-decimal-地方 - 本地/ 38982343 – Mojtaba