English 中文(简体)
如何将案文作为货币加以编排
原标题:How to format text as currency

我有一个假想,即以货币表示的表格编号,插入“ de”;数以千计的分离器:

1000     -> 1.000
10000,5  -> 10.000,5

我这样说:

$( #input ).myPlugin() 

但是,我要说的是,它明确了诽谤者和数千分离者所使用的特性:

$( #input ).myPlugin({thousand_type:  ,  , decimal_type  . }). 

我怎么能用我的gin子来做到这一点?


Here s the plugin:

(function( $ ){

  $.fn.myPlugin = function() {
    $( input ).keypress(function(event){
        if ((event.which  < 48 || event.which  > 57) && event.which  != 8 && event.which  != 44)
        event.preventDefault();
    });
    $( input ).keyup(function(event) {
        event.preventDefault();  

        val = $(this).val();

        if(val !=   ){
            thousand_type =  . ;        
            if(thousand_type ==  . ){
                decimal_type =  , ;
            }else{
                decimal_type =  . ;
            }

            //remove thousand mark
            if(thousand_type ==  . ){
                val = String(val).replace(/./g, "");
            }else{
                val = String(val).replace(/,/g, "");
            }

            //get position of decimal mark
            pos_decimal = String(val).indexOf(decimal_type);

            //device the number to thousand and decimal
            if(pos_decimal > -1){
                sub_decimal = String(val).substring(pos_decimal, String(val).length);
                sub_thousand = String(val).substring(0, pos_decimal);
            }else{
                sub_decimal =   ;
                sub_thousand = val;
            }
            //1.111.111,33
            //remove decimal mark
            if(decimal_type ==  . ){
                removed_mark_val = String(val).replace(/./g, "");
            }else{
                removed_mark_val = String(val).replace(/,/g, "");
            }

            //check is Numeric
            result = IsNumeric(removed_mark_val);

            if(result == true){
                if(thousand_type ==  . ){
                    sub_thousand = String(sub_thousand).split("").reverse().join("")
                      .replace(/(.{3}B)/g, "$1" + thousand_type)
                      .split("").reverse().join("");
                }else{              
                    sub_thousand = String(sub_thousand).split("").reverse().join("")
                      .replace(/(.{3}B)/g, "$1" + thousand_type)
                      .split("").reverse().join("");
                }
                val = sub_thousand + sub_decimal;
                //1111111,33
                $(this).attr( value ,val);  
            }else{

            }
        }
    });
    function IsNumeric(input){
        var RE = /^-{0,1}d*.{0,1}d+$/;
        return (RE.test(input));
    }
  };
})( jQuery );
问题回答

在这里,我写了一篇论文(319篇论文):

(function($)
{
    $.fn.myPlugin = function(options)
    {
        options = $.extend({
            thousands:  , ,
            decimal:  . 
        }, options);

        return this.keyup(function()
        {
            $(this).val(function(el, val)
            {
                val = val.replace(/[^d.,]/g,   ).split(options.decimal);
                val[0] = val[0].replace(options.decimal ===  .  ? /,/g : /./g,   );
                val[0] = val[0].replace(/(d)(?=(d{3})+$)/g, "$1" + options.thousands);
                return val.join(options.decimal);
            });
        });
    };

})(jQuery);

如此:

$( input ).myPlugin({
    thousands:  . ,
    decimal:  , 
});

见本文中的行动:。 http://jsfiddle.net/ywc7/


Note:,这仍然需要一些工作,因为arrow的钥匙非常高。 在我的项目中,我使用这一工具,把它打入<代码>blur活动。 但是,由于你似乎想要在<条码>关键上登出,因此,需要一些工作才能与arrow子一道玩.。

If you want to pass parameters to your jQuery plugin, simply accept parameters to your plugin function.

$.fn.myPlugin = function(opts) {

    ...

    if (opts.thousands_type ==  , ) {

    ...

详见





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签