Change the currency symbol or remove it in the inputmask currency

落花浮王杯 提交于 2021-01-22 05:22:08

问题


I'm using Robin Herbot's inputmask jquery plugin and I want to change the default currency symbol (by default, its a dollar currency symbol) to a PESO currency symbol or remove the currency symbol.

Below is what I've tried, yes the symbol changes and the symbol is removed but it won't let me type anything.

$(document).ready(function(){
  
  $("#currency1").inputmask({ alias : "currency", mask : "0.00" });
  $("#currency2").inputmask({ alias : "currency", mask : "₱ 0.00" });

  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.2.6/jquery.inputmask.bundle.min.js"></script>

<label>REMOVE CURRENCY SYMBOL</label><br>
<input type="text" id="currency1" /><br>
<label>CHANGE THE CURRENCY SYMBOL</label><br>
<input type="text" id="currency2" />

回答1:


You can change the symbol by using the prefix option. Below is a snippet where I do this in two different ways, modifying the currency alias and defining my own alias.

In your version you couldn't type anything since the mask property is used to restrict input and setting it to 0.00 only allows those four characters to be entered and nothing else. A mask of 9.99 would allow a number followed by a period and two numbers. 9 has a special masking definition allowing any number.

Inputmask.extendAliases({
  pesos: {
            prefix: "₱ ",
            groupSeparator: ".",
            alias: "numeric",
            placeholder: "0",
            autoGroup: !0,
            digits: 2,
            digitsOptional: !1,
            clearMaskOnLostFocus: !1
        }
});

$(document).ready(function(){
  
  $("#currency1").inputmask({ alias : "currency", prefix: '' });
  $("#currency2").inputmask({ alias : "currency", prefix: '₱ ' });
  $("#currency3").inputmask({ alias : "pesos" });

  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.2.6/jquery.inputmask.bundle.min.js"></script>

<label>REMOVE CURRENCY SYMBOL</label><br>
<input type="text" id="currency1" /><br>

<label>CHANGE THE CURRENCY SYMBOL</label><br>
<input type="text" id="currency2" /><br>

<label>CHANGE THE CURRENCY SYMBOL, using an alias</label><br>
<input type="text" id="currency3" />



回答2:


Solution with data-inputmask attribute

$(document).ready(function(){
  $("input").inputmask();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.2.6/jquery.inputmask.bundle.min.js"></script>

<label>REMOVE CURRENCY SYMBOL</label><br>
<input type="text" id="currency1" data-inputmask="'alias': 'decimal', 'groupSeparator': ',', 'autoGroup': true, 'digits': 2, 'digitsOptional': false, 'placeholder': '0'" style="text-align: right;"><br>
<label>CHANGE THE CURRENCY SYMBOL</label><br>
<input type="text" id="currency2" data-inputmask="'alias': 'decimal', 'groupSeparator': ',', 'autoGroup': true, 'digits': 2, 'digitsOptional': false, 'prefix': '₱ ', 'placeholder': '0'" style="text-align: right;">


来源:https://stackoverflow.com/questions/35008984/change-the-currency-symbol-or-remove-it-in-the-inputmask-currency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!