【问题标题】:Bootstrap TouchSpin + NumeralJS using locale pt-br is not working使用语言环境 pt-br 的 Bootstrap TouchSpin + NumeralJS 不起作用
【发布时间】:2019-01-12 03:24:52
【问题描述】:

我正在尝试使用bootstrap touchspin + numeraljs 将我的货币输入位置设置为pt-br

为此,我通过NumeralJS 注册了一个自定义语言环境,因此当我使用创建incrementdecrement 函数的语言环境时无法正常工作。如果我使用语言环境en,则功能完美。

使用的javascript代码如下:

	(function(global, factory) {
	  if (typeof define === 'function' && define.amd) {
	    define(['../numeral'], factory);
	  } else if (typeof module === 'object' && module.exports) {
	    factory(require('../numeral'));
	  } else {
	    factory(global.numeral);
	  }
	}(this, function(numeral) {
	  numeral.register('locale', 'pt-br', {
	    delimiters: {
	      thousands: '.',
	      decimal: ','
	    },
	    abbreviations: {
	      thousand: 'mil',
	      million: 'milhões',
	      billion: 'b',
	      trillion: 't'
	    },
	    ordinal: function(number) {
	      return 'º';
	    },
	    currency: {
	      symbol: 'R$'
	    }
	  });
	}));


	numeral.locale('pt-br');
	$("input[name='demo_callback']").TouchSpin({
	  callback_before_calculation: function(v) {

	    return numeral(v).value();
	  },
	  callback_after_calculation: function(v) {

	    return numeral(v).format("$0,0.00");
	  }
	});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://use.fontawesome.com/releases/v5.0.8/css/all.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-touchspin/4.2.5/jquery.bootstrap-touchspin.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>


<input id="demo_callback" type="text" value="1.00" data-bts-min="-1" data-bts-max="1000000" data-bts-step=".01" data-bts-decimals="2" name="demo_callback" />

最好的问候

【问题讨论】:

    标签: javascript html bootstrap-4 numeral.js


    【解决方案1】:

    感谢所有试图提供帮助的人,

    我能够通过将语言环境en小数分隔符替换为语言环境pt-br 的小数分隔符,然后通过NumeralJS 将其发送到转换来解决问题:

    	(function(global, factory) {
    	  if (typeof define === 'function' && define.amd) {
    	    define(['../numeral'], factory);
    	  } else if (typeof module === 'object' && module.exports) {
    	    factory(require('../numeral'));
    	  } else {
    	    factory(global.numeral);
    	  }
    	}(this, function(numeral) {
    	  numeral.register('locale', 'pt-br', {
    	    delimiters: {
    	      thousands: '.',
    	      decimal: ','
    	    },
    	    abbreviations: {
    	      thousand: 'mil',
    	      million: 'milhões',
    	      billion: 'b',
    	      trillion: 't'
    	    },
    	    ordinal: function(number) {
    	      return 'º';
    	    },
    	    currency: {
    	      symbol: 'R$'
    	    }
    	  });
    	}));
    
    
    	numeral.locale('pt-br');
    	$("input[name='demo_callback']").TouchSpin({
    	  callback_before_calculation: function(v) {
    
    	    return numeral(v.replace('.', ',')).value();
    	  },
    	  callback_after_calculation: function(v) {
    
    	    return numeral(v.replace('.', ',')).format("$0,0.00");
    	  }
    	});
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <link href="https://use.fontawesome.com/releases/v5.0.8/css/all.css" rel="stylesheet"/>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-touchspin/4.2.5/jquery.bootstrap-touchspin.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
    
    
    <input id="demo_callback" type="text" value="1.00" data-bts-min="-1" data-bts-max="1000000" data-bts-step=".01" data-bts-decimals="2" name="demo_callback" />

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      相关资源
      最近更新 更多