如何显示文本字段中输入的数字?

时间:2011-10-12 14:14:28

标签: javascript jquery

我正在创建一个带有一个字段的表单来输入数字。如何在下面的文本字段中以文本格式显示数字的值?

例如:如果我在文本字段中输入数字2000,则数字会在下面显示为单词(“两千”)。

3 个答案:

答案 0 :(得分:2)

你试过谷歌吗? Second hit

答案 1 :(得分:0)

我猜你可以使用第一个文本框中的“KeyUp”函数,并使用case语句来填充第二个文本框。像下面的东西。

  $(document).ready(function{} {
       $("#txt1").keyup(function() {

            case ()....           
        });
  });

答案 2 :(得分:0)

你在这里

function readable_number(num){
    var bits_a = ["thousand", "million", "billion", "trillion", "zillion"];
    var bits_b = ["ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"];
    var bits_c = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];

    if (num==0){return 'zero';}

    var result = (num<0)?'minus ':'';

    num = Math.abs(num);
    for(var i=bits_a.lenght; i>0; i--){
        var p = pow(1000, i);
        if (num > p){
            var b = Math.floor(num/p);
            num -= p * b;
            result += readable_number(b) + ' ' + bits_a[i-1];
            result += ((num)?', ':'');
        }
    }
    if (num > 100){
        var b = Math.floor(num/100);
        num -= 100 * b;
        result += readable_number(b) + ' hundred' + ((num)?' and ':' ');
    }
    if (num >= 20){
        var b = Math.floor(num/10);
        num -= 10 * b;
        result += bits_b[b-1] + ' ';
    }
    if (num){
        result += bits_c[num-1];
    }
    return result;
}

alert(readable_number(1234));