Selon les besoins des utilisateurs, le montant doit être formaté lors de l'entrée, c'est-à-dire que tous les trois chiffres sont séparés par des virgules et deux décimales sont conservées.
Compte tenu de l'expérience de l'utilisateur, formatez le montant avec JS, le code de premier plan est le suivant:
La copie de code est la suivante:
<asp: textbox id = "txtamount" runat = "server" onKeyPress = "check ()" onkeyup = "run (this)"> </ asp: textbox>
Le code JS est le suivant:
La copie de code est la suivante:
// ====== Vérifiez si l'entrée est un nombre
Fonction Check () {
if (! ((window.event.KeyCode> = 48 && window.event.KeyCode <= 57) || window.event.keycode == 46 || window.event.keycode == 45)) {
window.event.KeyCode = 0
}
}
// ======== La quantité de zone de texte formatée
fonction run (obj) {
var objvalue = obj.value.replace (/ [,] / g, ""),
objLength = objvalue.length,
dtmp = objvalue.indexof ("."),
neg = objvalue.indexof ("-");
var inttmp = 0,
FloATTMP = -1;
if (dtmp! = -1) {
inttmp = dtmp == 0? "0": new String (objValue) .SubString (0, dtmp);
floattmp = new String (objvalue) .substring (dtmp + 1, objLength + 1);
FloattMP = FloattMP.replace (/ [^ 0-9] / g, "");
}
autre {
inttmp = objvalue;
}
if (neg == 0) {
inttmp = inttmp.replace (/ [-] / g, "");
}
inttmp = inttmp.replace (/ [^ 0-9] / g, "");
var tmp = "", str = "0000";
pour (; inttmp.length> 3;) {
var temp = new String (inttmp / 1000);
if (temp.indexof (".") == -1) {
tmp = ", 000" + tmp;
inttmp = temp;
}
autre {
var le = new String (temp) .split (".") [1] .length;
tmp = "," + nouvelle chaîne (temp) .split (".") [1] + str.substring (0, 3 - le) + tmp;
inttmp = new String (temp) .split (".") [0];
}
}
inttmp = inttmp + tmp;
obj.value = nég == 0? "-" + inttmp + running (floattmp): inttmp + running (floattmp);
}
// ======= Organisez la partie décimale
fonction en cours d'exécution (val) {
if (val! = "-1" && val! = "") {
var valvalue = 0 + "." + Val;
if (val.length> = 2) {
valvalue = parsefloat (valvalue) .tofixed (2);
}
var temp = "." + valvalue.split (".") [1];
Tempère de retour;
}
else if (val! = "0" && val == "") {
retour ".";
}
autre {
retour "";
}
}
Dans le même temps, puisque le montant peut être saisi dans un nombre négatif, le jugement de "neg = objvalue.indexof (" - ")" est ajouté.
En ce qui concerne le formatage du montant, je rencontre souvent de telles choses. Si je pense que c'est OK, je vais le garder pour un accès facile à l'avenir!