这就是我现在所拥有的:
$("#number").val(parseFloat($("#number").val()).toFixed(2));
我觉得很乱。我认为我没有正确链接这些函数。我是否必须为每个文本框调用它,或者我可以创建一个单独的函数吗?
如果您要对多个领域执行此操作,或者经常执行此操作,那么插件可能就是答案。
这是 jQuery 插件的开始,它将字段的值格式化为小数点后两位。
它是由字段的onchange事件触发的。你可能想要一些不同的东西。
<script type="text/javascript">
// mini jQuery plugin that formats to two decimal places
(function($) {
$.fn.currencyFormat = function() {
this.each( function( i ) {
$(this).change( function( e ){
if( isNaN( parseFloat( this.value ) ) ) return;
this.value = parseFloat(this.value).toFixed(2);
});
});
return this; //for chaining
}
})( jQuery );
// apply the currencyFormat behaviour to elements with 'currency' as their class
$( function() {
$('.currency').currencyFormat();
});
</script>
<input type="text" name="one" class="currency"><br>
<input type="text" name="two" class="currency">
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)