我有一个假想,即以货币表示的表格编号,插入“ 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 );