用千位分隔符求和

时间:2018-11-19 08:48:55

标签: javascript jquery

卡住了数千个分隔符。当我在输入框中设置一个值时,只有第一个数字组会在结果框中汇总。如果有千位分隔符,如何正确求和整个值? 预先感谢

$("input").change(function () {   

  var c = parseInt($("input[name=cp]").val()),
  a = parseInt($("input[name=ae]").val()),
  d = parseInt($("input[name=d]").val()),
  cs = parseInt ($("input[name=cs]").val());

  var result = ( ( c + a ) - d + cs );

  $("#r").val( result );

}); // end change

$("input[name=cp]").on("keyup", function () {

  this.value = this.value.replace(/ /g,'');
  var number = this.value;
  this.value = number.replace(/\B(?=(\d{3})+(?!\d))/g, " ");

}); // end on cp

$("input[name=ae]").on("keyup", function () {

  this.value = this.value.replace(/ /g,'');
  var number = this.value;
  this.value = number.replace(/\B(?=(\d{3})+(?!\d))/g, " ");

}); // end on ae

$("input[name=d]").on("keyup", function () {

  this.value = this.value.replace(/ /g,'');
  var number = this.value;
  this.value = number.replace(/\B(?=(\d{3})+(?!\d))/g, " ");

}); // end on d

                            
$("input[name=cs]").on("keyup", function () {

  this.value = this.value.replace(/ /g,'');
  var number = this.value;
  this.value = number.replace(/\B(?=(\d{3})+(?!\d))/g, " ");

}); // end on cs
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text"   name="cp" value="0" maxlength="8" >
<input type="text" name="ae" value="0" maxlength="7" >
<input type="text" name="d" value="0" maxlength="8" >
<input type="text" name="cs" value="0" maxlength="7" >
<input type="text" id="r">

2 个答案:

答案 0 :(得分:3)

praseInt: Description

  

如果praseInt在指定的基数中遇到的字符不是数字,它将忽略该字符和所有后续字符,并返回到该点为止已解析的整数值。 praseInt将数字截断为整数值。允许使用前导和尾随空格。

使用praseInt之前,您必须替换值中的空格:

var c = parseInt($("input[name=cp]").val().replace(/ /g,'')),
a = parseInt($("input[name=ae]").val().replace(/ /g,'')),
d = parseInt($("input[name=d]").val().replace(/ /g,'')),
cs = parseInt ($("input[name=cs]").val().replace(/ /g,''));

工作代码示例:

$("input").change(function () {   
  var c = parseInt($("input[name=cp]").val().replace(/ /g,'')),
  a = parseInt($("input[name=ae]").val().replace(/ /g,'')),
  d = parseInt($("input[name=d]").val().replace(/ /g,'')),
  cs = parseInt ($("input[name=cs]").val().replace(/ /g,''));
  var result = ( ( c + a ) - d + cs );

  $("#r").val( result );
}); // end change

$("input[name=cp]").on("keyup", function () {
  this.value = this.value.replace(/ /g,'');
  var number = this.value;
  this.value = number.replace(/\B(?=(\d{3})+(?!\d))/g, " ");                     
}); // end on cp

$("input[name=ae]").on("keyup", function () {
  this.value = this.value.replace(/ /g,'');
  var number = this.value;
  this.value = number.replace(/\B(?=(\d{3})+(?!\d))/g, " ");

}); // end on ae

$("input[name=d]").on("keyup", function () {

  this.value = this.value.replace(/ /g,'');
  var number = this.value;
  this.value = number.replace(/\B(?=(\d{3})+(?!\d))/g, " ");

}); // end on d

                            
$("input[name=cs]").on("keyup", function () {

  this.value = this.value.replace(/ /g,'');
  var number = this.value;
  this.value = number.replace(/\B(?=(\d{3})+(?!\d))/g, " ");

}); // end on cs
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text"   name="cp" value="0" maxlength="8" >
<input type="text" name="ae" value="0" maxlength="7" >
<input type="text" name="d" value="0" maxlength="8" >
<input type="text" name="cs" value="0" maxlength="7" >
<input type="text" id="r">

答案 1 :(得分:1)

function getValue(val) {
  return parseInt(val.replace(/[^\d]+/g,''));
}

$("input").change(function () {
  var c = getValue($("input[name=cp]").val()),
  a = getValue($("input[name=ae]").val()),
  d = getValue($("input[name=d]").val()),
  cs = getValue ($("input[name=cs]").val());
  var result = String( ( c + a ) - d + cs ).replace(/\B(?=(\d{3})+(?!\d))/g, " ");
  $("#r").val( result );

}); // end change

$("input[name=cp], input[name=ae], input[name=d], input[name=cs]").on("keyup", function () {
  this.value = String(getValue(this.value)).replace(/\B(?=(\d{3})+(?!\d))/g, " ");
}); // end on all
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text"   name="cp" value="0" maxlength="8" >
<input type="text" name="ae" value="0" maxlength="7" >
<input type="text" name="d" value="0" maxlength="8" >
<input type="text" name="cs" value="0" maxlength="7" >
<input type="text" id="r">

这是什么?