允许数字中的十进制值

时间:2017-07-28 12:05:33

标签: jquery decimal

我已经提交了一些申请表格。金额以逗号分隔,不允许小数。我想允许十进制数量。提前谢谢......

这是我的代码:

(function($, undefined) {

    "use strict";


    $(function() {

        var $form = $( ".number" );
        var $input = $form.find( ".no" );

        $input.on( "keyup", function( event ) {

            var selection = window.getSelection().toString();
            if ( selection !== '' ) {
                return;
            }


            if ( $.inArray( event.keyCode, [38,40,37,39] ) !== -1 ) {
                return;
            }


            var $this = $( this );


            var input = $this.val();

            var input = input.replace(/[\D\s\._\-]+/g, "");
            input = input ? parseInt( input, 10 ) : 0;

            $this.val( function() {
                return ( input === 0 ) ? "" : input.toLocaleString( "en-US" );
            });

        });



    });

})(jQuery);

1 个答案:

答案 0 :(得分:0)

$(function() {

var $form = $( ".number" );
var $input = $form.find( ".no" );

$input.on( "keyup", function( event ) {



    var selection = window.getSelection().toString();
    if ( selection !== '' ) {
        return;
    }


    if ( $.inArray( event.keyCode, [38,40,37,39] ) !== -1 ) {
        return;
    }


    var $this = $( this );


    var input = $this.val();

            input = input ? parseFloat( input) : 0;

            $this.val( function() {
                return ( input === 0 ) ? "" : input.toLocaleString( "en-US" );
            } );
} );



  });
   })(jQuery);