简体   繁体   English

如果第一个数字为零且该数字大于2位,则替换

[英]Replacing if the first number is a zero and the number is greater than 2 places

I have a calculator I'm working on and came across a problem. 我有一个正在使用的计算器,遇到了一个问题。 To combat so that users can't leave a field blank, I'm forcing a zero if the field is left empty. 为了避免用户不能将字段留空,如果字段为空,我将强制为零。 That's all fine, but the problem is that when the text in the field is deleted to remove the zero and enter a new number, it automatically enters zero so my new number looks like this: 05 很好,但是问题是,当删除字段中的文本以删除零并输入新数字时,它会自动输入零,因此我的新数字如下所示:05

How do i run a replace where if there is more than 2 places in the number and the first number is zero, replace the zero? 如果号码中有2个以上的地方并且第一个数字为零,那么我应该如何执行替换?将零替换为零? Here's the code i'm using for my calculator. 这是我用于计算器的代码。

$(function(){

  calculate();

$('.input').keypress(function (e) {
     //if the letter is not digit then display error and don't type anything
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
               return false;
    }
   });

  $('.input').on('keyup',function(){
    if($(this).val()==''){
      $(this).val('0');
    }
    calculate();
  });

});

function calculate(){

  var d6 = parseFloat(($('#d6').val()).replace(/,/g,''));
  var d20 = parseFloat(($('#d20').val()).replace(/,/g,''));
  var b20 = d6;
  var e20 = parseFloat(($('#e20').val()).replace(/,/g,''));

  var f20 = d20*e20;
  var f22 = b20/f20;
  var f23 = (52-f22)*f20;

  $('#f20').html(formatCurrency(f20));
  $('#f22').html(f22.toFixed(2));
  $('#f23').html(formatCurrency(f23));

}

function formatCurrency(x) {
    return '$'+x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
}

更改调零代码以使用blur()事件,即当该字段失去焦点时。

$('.input').blur(function(){
if($(this).val()=='') { $(this).val('0'); } });

Instead of using keyup and keypress event for checking and replacing blank to zero, use change event. 代替使用keyup和keypress事件来检查和将空白替换为零,请使用change事件。

$(function(){

  calculate();

$('.input').keypress(function (e) {
     //if the letter is not digit then display error and don't type anything
     if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
               return false;
    }
   });

  $('.input').on('keyup',function(){
    calculate();
  });

$('.input').on('change',function(){
    if($(this).val()==''){
      $(this).val('0');
    }
  });

});

function calculate(){
var d6Val = ($('#d6').val() !== "")? $('#d6').val() : '0';
var d20Val = ($('#d20').val() !== "")? $('#d20').val() : '0';
var e20Val = ($('#e20').val() !== "")? $('#e20').val() : '0';
  var d6 = parseFloat((d6Val).replace(/,/g,''));
  var d20 = parseFloat((d20Val).replace(/,/g,''));
  var b20 = d6;
  var e20 = parseFloat((e20Val).replace(/,/g,''));

  var f20 = d20*e20;
  var f22 = b20/f20;
  var f23 = (52-f22)*f20;

  $('#f20').html(formatCurrency(f20));
  $('#f22').html(f22.toFixed(2));
  $('#f23').html(formatCurrency(f23));

}

function formatCurrency(x) {
    return '$'+x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
}

One more thing. 还有一件事。 Change event only fires when you focus-out from that input. 仅当您从该输入聚焦时才会触发Change事件。

Please let me know if you will face any issue. 如果您遇到任何问题,请告诉我。

If you are essentially trying to turn it into a formatted number you could try type coercion: 如果您本质上是试图将其转换为带格式的数字,则可以尝试键入强制:

'' + new Number('')       // "0"
'' + new Number('0')      // "0"
'' + new Number('05')     // "5"
'' + new Number('0000.2') // "0.2"

I'm assuming that the text is removed from pressing the backspace key. 我假设从按Backspace键删除了该文本。

If that is the case then you keyup handler would fire when you backspace on the zero, which would detect no input, then add the zero. 如果是这种情况,那么当您将退格键设置为零时,将触发键控处理程序,这将检测不到输入,然后添加零。

well maybe this will do. 好吧,也许这样做。

$('.input').on('keyup',function(){
        if($(this).val()=='' && $(this).val().substring(0,2) === ' ' || '') {
         $(this).val('0');
        }
        calculate();
      });

    });

First of all, you are doing it a hard way. 首先,您很难做到这一点。 And try this... if the user clicks on the input then it will be cleared and the user can write whatever number he wants... 并尝试此操作...如果用户单击输入,则将清除该输入,并且用户可以输入所需的任何数字...

$( ".input" ).focus(function() {
  (this).val('');
});

In case you are using an HTML5 form you can avoid that piece of code like this: 如果您使用的是HTML5表单,则可以避免使用以下代码:

<input type="number" placeholder="Type a number" required>

The required attribute is a boolean attribute. 必需属性是布尔属性。

When present, it specifies that an input field must be filled out. 如果存在,它指定必须填写输入字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM