0

数値の入力中に自動的にデシメルに変更される入力を作成しようとしていますが、2デシメルポイントのみで修正されています。私を助けてください、私はまだjqueryで新しいので

私のコード:

 $.fn.billFormat = function() {
        $(this).keyup( function( e ){
                if( isNaN(parseFloat(this.value )) ) return;
                this.value = parseFloat(this.value).toFixed(2);
        });
        return this; 
 }

$('#ENQUIRY_PREPAIDBILL .bill_issued').billFormat();

このように出力したい

0.00<------デフォルト値
0.01<------入力
10.10<------入力
01.00<------入力0

4

2 に答える 2

2
 $.fn.billFormat = function() {
        this.val("0.00");
        this.keyup( function( e ){
                if( isNaN(parseFloat(this.value )) ) return;
                this.value = parseFloat(this.value * 10, 10).toFixed(2);
        });
        return this; 
 }

これがデモです。

于 2012-06-15T02:52:44.850 に答える
0

これはオーバーフローを心配しない別の解決策です(ここでデモを見つけることができます

$(document).ready(function(){
    $('#txtInput').keydown(function(e){
        var value = $('#txtInput').val().toString();
        var number = null;
        if(value == '')
            value = '0.00';
        switch(e.which)
        {
            case 48:
            case 49:
            case 50:
            case 51:
            case 52:
            case 53:
            case 54:
            case 55:
            case 56:
            case 57:
                number = e.which - 48;
            case 8:
            case 46:
            case 32:
            case 37:
            case 38:
            case 39:
            case 40:
                break;
            default:
                e.preventDefault();
                break;
        }
        var dotIndex = value.indexOf('.');
        if(number === null)
        {
            value = value.substr(0,dotIndex-1) + '.' + value.substr(dotIndex -1,1) + value.substr(dotIndex+1,1);
            if(value.indexOf('.') ==0)
            {
                value = '0' + value;
            }
        }
        else
        {

            value = value.substr(0,dotIndex) + value.substr(dotIndex+1,1) + '.' + value.substr(dotIndex+2);

            value += number.toString();  
            value = value.replace(/^0+/,'');
            if(value.indexOf('.') == 0)
            {
                value = '0' + value;
            }               

        }
        $('#txtInput').val(value);
        e.preventDefault();
    }).keyup(function(e){

        switch(e.which)
        {
            case 48:
            case 49:
            case 50:
            case 51:
            case 52:
            case 53:
            case 54:
            case 55:
            case 56:
            case 57:
                e.preventDefault();
                break;
        }
    });
});​

およびこのコードのhtml

<input type='text' id='txtInput' />​
于 2012-06-15T04:19:00.987 に答える