添加千分位
{ data: "unit",
render: function (data, type, row) {return data && (data.toString().indexOf('.') != -1 ? data.toString().replace(/(\d)(?=(\d{3})+\.)/g, function($0, $1) {return $1 + ",";}) : data.toString().replace(/(\d)(?=(\d{3})+$)/g, function($0, $1) { return $1 + ","; }));
}
},
$('#productWeight').val(pUnit[0]);
var val1=document.getElementById("productWeight").value;
val1 = val1 && (val1.toString().indexOf('.') != -1 ? val1.toString().replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) { return $1 + ","; }) : val1.toString().replace(/(\d)(?=(\d{3})+$)/g, function ($0, $1) { return $1 + ","; }));
document.getElementById("productWeight").value=val1;
加千位分隔符后去千位分隔符
{ data: "HandNumber",
render: function (data, type, row) {
var gg= data && (data.toString().indexOf('.') != -1 ? data.toString().replace(/(\d)(?=(\d{3})+\.)/g, function($0, $1) {return $1 + ",";}) : data.toString().replace(/(\d)(?=(\d{3})+$)/g, function($0, $1) { return $1 + ","; }));
var arrayObj = new Array();
arrayObj = gg.split(',');
var num=arrayObj.length;
var str1='';
for (i=0; i <= num-1; i++) {
str1 += arrayObj[i];
}
data=str1;
return data;
}
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
- <script type="text/javascript">
- //控制输入框只能输入数字和小数点
- function limit(id){
- var limitV=$("#"+id).val();
- $("#"+id).val(limitV.replace(/[^0-9.]/g,""));
- format(id);
- }
- //对输入数字的整数部分插入千位分隔符
- function format(id){
- var formatV=$("#"+id).val();
- var array=new Array();
- array=formatV.split(".");
- var re=/(-?\d+)(\d{3})/;
- while(re.test(array[0])){
- array[0]=array[0].replace(re,"$1,$2")
- }
- var returnV=array[0];
- for(var i=1;i<array.length;i++){
- returnV+="."+array[i];
- }
- $("#"+id).val(returnV);
- }
- //过滤掉数据的千位分隔符
- function unformat(id){
- var v=$("#"+id).val();
- var x = v.split(',');
- var returnV=parseFloat(x.join(""));
- $("#"+id).val(returnV);
- }
- </script>
- </head>
- <body>
- <input type="text" name="dy" id="dy" onkeyup="limit('dy')" onFocus="format('dy')" onBlur="unformat('dy')"/>
- </body>
- </html>