我一直在搞乱autoNumeric,这是一个用于格式化货币字段的 jQuery 插件。
我想连接插件,以便在用户看到页面时(例如加载时)对所有货币字段进行格式化。
目前,我似乎无法解决的默认设置是字段根据字段本身的模糊、键或其他操作进行格式化。
我一直在试验插件代码,看起来这个相对较新的人需要一些时间来解决这个问题,如果有的话。
有人在这吗?
里尔
我一直在搞乱autoNumeric,这是一个用于格式化货币字段的 jQuery 插件。
我想连接插件,以便在用户看到页面时(例如加载时)对所有货币字段进行格式化。
目前,我似乎无法解决的默认设置是字段根据字段本身的模糊、键或其他操作进行格式化。
我一直在试验插件代码,看起来这个相对较新的人需要一些时间来解决这个问题,如果有的话。
有人在这吗?
里尔
触发 'focusout' 事件会格式化该字段。触发“更改”在最新版本 (1.7.4) 中不起作用。
$('input.money').autoNumeric({aNeg: '-'}).trigger('focusout');
autoNumeric 在 'onchange' 事件触发后进行所有格式化。因此,您只需要以编程方式触发此事件。像这样:
$('input.money').autoNumeric({aNeg: '-'}).trigger('change');
希望这可以帮助!
我自己也遇到了这个问题。我必须使它更通用,但这对我有用:
$('input.auto-numeric').ready(function(){
var format_options = {
aSign: '$'
};
$('input.auto-numeric').each(function(){
$(this).autoNumeric(format_options);
if($(this).attr('id')){
$(this).val($.fn.autoNumeric.Format($(this).attr('id'), $(this).val(), format_options));
}
});
});
这就是我最终所做的:
$(document).ready(function(){
$('input.auto').autoNumeric();
$('input.auto').each(function(){
var element = this
if(element.value !=""){
$('#'+element.id).val($.fn.autoNumeric.Format(element.id, element.value));
}
}
);
});
这应该有效。
jQuery(function($) {
$('input.auto').ready(function(){
$('input.auto').autoNumeric();
var inputID = uniqueID; // use the jQuery.get() function to retrieve data
var formatValue = '1234.00'; // use the jQuery.get() function to retrieve data
if(jQuery().autoNumeric){
$('#id').val($.fn.autoNumeric.Format(inputID, formatValue));
}
else{
alert('plugin not available');
}
});
});
鲍勃
强制格式化的另一种方法是使用“更新”,例如
$(".input-numeric").autoNumeric('update');
在当前版本2.*
及以后的版本中,由于formatOnPageLoad
设置为true
.
就这么简单;)