jQuery onkeypress将数字格式更改为1,000.00

时间:2019-02-20 08:15:27

标签: jquery onchange onkeypress

如果用户键入1000,它将自动更改为1,000.00。我当前的代码仅更改为1000.00。请帮助我。

function isNumberKey(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode(key);
  if (key.length == 0) return;
  var regex = /^[0-9.,\b]+$/;
  if (!regex.test(key)) {
    theEvent.returnValue = false;
    if (theEvent.preventDefault) theEvent.preventDefault();
  }
}

function toFloat(z) {
  var x = document.getElementById(z);
  x.value = parseFloat(x.value).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
}

$( ".fn" ).keyup(function() {
<input onchange="toFloat('nmi')" type="text" maxlength="10" onkeypress="return isNumberKey(event)" required="required" class="form-control fn" placeholder="RM" name="nmi" id="nmi">

<input onchange="toFloat('a')" type="text" maxlength="10" onkeypress="return isNumberKey(event)" required="required" class="form-control fn" placeholder="RM" name="a" id="a">

0 个答案:

没有答案