2010-05-19 12 views
0

Je développe un nouveau site, et ici, il est possible pour l'utilisateur de saisir un prix dans un champ de saisie. Ce dont j'ai besoin, c'est d'un code jQuery pour transformer le prix tapé. Cela signifie que lorsqu'un utilisateur tape "1000", le texte visible dans le champ de saisie sera automatiquement transformé en "1.000" - avec un point. Et si elles tapent "10000" cela devrait être transformé en "10.000" etc. Vous pouvez voir un exemple réel sur ce site que j'ai trouvé: http://www.boligsiden.dk/ Je sais que c'est sur le danois, mais sur la première page sous la carte, il y a pour entrer des champs. L'un d'eux dit "Minimum kontaktpris". Vous pouvez essayer de saisir un nombre ici, et voir l'effet que je cherche.Séparateur de nombre de jQuery - Séparez le nombre par chaque 1.000

Est-ce que quelqu'un sait comment je peux y parvenir? Soit par un plugin jQuery, soit par un code "fait maison"?

Répondre

2
+0

Cela ressemble à un outil sympa, mais je ne vois aucune démonstration de cela, donc je ne sais pas comment l'utiliser. Comment puis-je connecter un couple de mes champs d'entrée à cet outil. Pourriez-vous donner un exemple de code? –

1

J'utiliser le format numérique dans les php.js: http://phpjs.org/functions/number_format:481

faire quelque chose comme ceci:

$(element).change(function() { 
    $(this).val(number_format($(this).val(), 0, '.', '.')); 
} 
+0

Cela fonctionnerait-il dans un environnement .NET? –

+0

Je ne travaille pas beaucoup sur .NET mais c'est complètement côté client, donc oui, ça devrait l'être. –

1

JavaScript: The Good Parts

Ne pas oublier de remercier http://www.boligsiden.dk

function NumericBox_KeyDown(o, e, separate, allowComma) { 
    o.onkeypress = o.onkeyup = null; 
    if (!e) e = window.event; 
    var code = e.keyCode; 
    if (!code && e.which) code = e.which; 
    if (code >= 96 && code <= 105) code -= 48; 
    var preventDefault = ((code > 57 && (code != 110 && code != 188)) || code == 32 || (code >= 48 && e.shiftKey)); 
    if (!allowComma && (code == 188 || code == 110)) preventDefault = true; 
    if (!preventDefault) { 
     if (separate) { 
      if (NumericBox_CanSelect(o)) { 
       if (((code >= 48 && code <= 57) || code == 8 || code == 46 || code == 110 || code == 188)) { 
        preventDefault = NumericBox_FormatNumber(o, code, allowComma); 
       } 
      } 
     } 
    } 
    if (preventDefault) { 
     o.onkeypress = o.onkeyup = function (e) { 
      if (!e) e = window.event; 
      return false; 
     }; 
     return false; 
    } 
    return true; 
} 

function NumericBox_CanSelect(o) { 
    return (typeof(o.createTextRange) != 'undefined' || typeof(o.selectionStart) != 'undefined'); 
} 

function NumericBox_FormatNumber(o, code, allowComma) { 
    if (!allowComma && (code == 188 || code == 110)) return true; 
    var preventDefault = false; 
    var startPos = getSelectionStart(o); 
    var endPos = getSelectionEnd(o); 
    var s = o.value; 

    //delete all non numbers except one optional comma 
    var i = o.value.length - 1; 
    while (i >= 0) { 
     var c = s.charAt(i); 
     if (c < '0' || c > '9') { 
      if (c == ',' && allowComma == true) { 
       allowComma = false; 
      } else { 
       s = s.substring(0, i) + s.substring(i + 1); 
       if (startPos > i) startPos--; 
       if (endPos > i) endPos--; 
      } 
     } 
     i--; 
    } 
    if (startPos == 1 && s.charAt(0) == '0' && code == 48) preventDefault = true; 
    if (startPos == 0 && s.length > 0 && code == 48) preventDefault = true; 
    while (s.length > 0 && s.charAt(0) == '0' && s.charAt(1) != ',' && code != 48) { 
     s = s.substring(1); 
     startPos--; 
     endPos--; 
    } 
    if (code == 188 || code == 110) { 
     preventDefault = !allowComma; 
     if (allowComma && startPos == 0) { 
      s = '0' + s; 
      startPos++; 
      endPos++; 
     } 
    } 

    var s2 = s.substring(0, startPos); 
    for (var k = startPos; k < endPos; k++) 
    s2 += 'B'; 
    s2 += s.substring(endPos); 
    var s3 = s2; 
    var s4 = s; 
    if (code >= 48 && code <= 57 && !preventDefault) { 
     s3 = s3.substring(0, startPos) + 'A' + s3.substring(startPos); 
     s4 = s4.substring(0, startPos) + 'A' + s4.substring(startPos); 
    } 
    if (code == 8 && startPos == endPos && !preventDefault) { 
     if (s3.charAt(startPos - 1) == ',') { 
      s3 = s3.substring(0, startPos - 1) + 'B' + s3.substring(startPos); 
     } else { 
      s3 = s3.substring(0, startPos) + 'C' + s3.substring(startPos); 
      s4 = s4.substring(0, startPos) + 'C' + s4.substring(startPos); 
     } 
    } 
    if (code == 46 && startPos == endPos && !preventDefault) { 
     if (s3.charAt(startPos) == ',') { 
      s3 = s3.substring(0, startPos) + 'B' + s3.substring(startPos + 1); 
     } else { 
      s3 = s3.substring(0, startPos + 1) + 'C' + s3.substring(startPos + 1); 
      s4 = s4.substring(0, startPos + 1) + 'C' + s4.substring(startPos + 1); 
     } 
    } 

    var commaPos = s3.indexOf(','); 
    if (commaPos < 0) commaPos = s3.length; 
    if ((code == 188 || code == 110) && !preventDefault) commaPos = startPos; 
    var j = 0; 
    for (var l = commaPos; l > 0; l--) { 
     if (s3.charAt(l) == 'C') j -= 2; 
     if (j > 2) { 
      if (l <= startPos) { 
       startPos++; 
       endPos++; 
      } 
      s4 = s4.substring(0, l) + '.' + s4.substring(l); 
      j = 0; 
     } 
     if (s3.charAt(l - 1) != 'B') j++; 
    } 

    o.value = s4.replace('A', '').replace('C', ''); 
    setCursorPosition(o, startPos, endPos); 
    if (typeof(document.execCommand) != 'undefined') try { 
     document.execCommand('OverWrite', false, false); 
    } catch (e) {} 
    return preventDefault; 
} 


function getSelectionStart(o) { 
    if (o.createTextRange) { 
     var r = document.selection.createRange().duplicate() 
     r.moveEnd('character', o.value.length) 
     if (r.text == '') return o.value.length 
     return o.value.lastIndexOf(r.text) 
    } else return o.selectionStart 
} 

function getSelectionEnd(o) { 
    if (o.createTextRange) { 
     var r = document.selection.createRange().duplicate() 
     r.moveStart('character', -o.value.length) 
     return r.text.length 
    } else return o.selectionEnd; 
} 
+0

Bonjour Anurag. Je viens d'essayer celui-ci, mais le seul problème que je peux voir est que quand je marque le texte dans le champ d'entrée, et appuie sur un nouveau numéro, le nouveau numéro est simplement inséré à la fin de l'ancien numéro. Comment se peut-il? Et peut-il être résolu? –