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

时间:2015-08-28 18:03:25

标签: javascript jquery

我有一个我正在研究的计算器并遇到了一个问题。为了使用户不能将一个字段留空而进行打击,如果该字段为空,我将强制为零。这一切都很好,但问题是,当删除字段中的文本以删除零并输入新数字时,它会自动输入零,所以我的新数字看起来像这样:05

我如何运行替换,如果数字中有多于2个位置并且第一个数字为零,则替换零?这是我用于计算器的代码。

$(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, ",");
}

7 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

不使用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, ",");
}

还有一件事。只有在您从该输入聚焦时才会触发更改事件。

如果您遇到任何问题,请与我们联系。

答案 2 :(得分:1)

如果您基本上试图将其转换为格式化数字,您可以尝试输入强制:

public EClusterOptionsProvider(Context context) {
    mClusterOptions = new ClusterOptions();
    mContext = context;
    mIconGenerator = new IconGenerator(context);

    // Inflate Layout
    View markerView = LayoutInflater.from(context).inflate(R.layout.map_marker, null);
    mImageView = (ImageView) markerView.findViewById(R.id.map_marker_image);
    mTextView = (TextView) markerView.findViewById(R.id.map_marker_text);

    // Setup Icon Generator
    mIconGenerator.setContentView(markerView);
}

public Bitmap createIcon(Bitmap bmp, String text) {
    mImageView.setImageBitmap(bmp);
    mTextView.setText(text);
    return mIconGenerator.makeIcon();
}

@Override
public ClusterOptions getClusterOptions(List<Marker> list) {
    // Get Bitmap from first marker
    Marker first = list.get(0);
    mClusterOptions.icon(BitmapDescriptorFactory.fromBitmap(mIconGenerator.makeIcon()));
    return mClusterOptions;
}

答案 3 :(得分:0)

我假设文本已从按退格键中删除。

如果是这种情况,那么当您在零上退格时会触发键盘处理程序,这会检测到没有输入,然后添加零。

答案 4 :(得分:0)

好吧也许这样做。

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

    });

答案 5 :(得分:0)

首先,你正在努力做到这一点。并尝试这个...如果用户点击输入,那么它将被清除,用户可以写出他想要的任何数字......

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

答案 6 :(得分:0)

如果您使用的是HTML5表单,可以避免使用这样的代码:

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

必需属性是布尔属性。

如果存在,则指定必须填写输入字段。