如何将文本格式设置为货币
我有一个插件,可以通过插入小数点和数字来将数字格式化为货币。千位分隔符:
1000 -> 1.000
10000,5 -> 10.000,5
我这样称呼它:
$('#input').myPlugin()
但我想通过指定用于小数和千位分隔符的字符来调用它:
$('#input').myPlugin({thousand_type: ',' , decimal_type '.'}).
如何在我的插件中实现这一点?
这是插件:
(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 );
I have a plugin that formats numbers as currency, by inserting decimal & thousands separators:
1000 -> 1.000
10000,5 -> 10.000,5
and I call it like this:
$('#input').myPlugin()
But I want to call it by specifying the characters to use for the decimal and thousands separators:
$('#input').myPlugin({thousand_type: ',' , decimal_type '.'}).
How can I accomplish that in my plugin?
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 );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想将参数传递给 jQuery 插件,只需接受插件函数的参数即可。
请参阅此处了解更多信息。
If you want to pass parameters to your jQuery plugin, simply accept parameters to your plugin function.
See here for more information.
这是我编写的一个插件(缩小为 319 字节):
像这样使用它:
在此处查看它的实际操作:http://jsfiddle。 net/yywc7/
注意:这仍然需要一些工作,因为箭头键非常混乱。我在我的项目中使用它,并将插件绑定到
blur
事件中。但由于您似乎希望在keyup
上使用它,因此需要一些工作才能与箭头键很好地配合。Here's a plugin I wrote (319 bytes minified):
Use it like this:
See it here in action: http://jsfiddle.net/yywc7/
Note: this still needs some work, as the arrow keys are pretty messed up. I use it in my projects with the plugin tying into the
blur
event. But since you seem to want it onkeyup
, it needs some work to play nice with the arrow keys.