fourcal/src/main/resources/static/assets/js/project_common.js

163 lines
4.5 KiB
JavaScript
Raw Normal View History

2021-11-05 09:01:26 +00:00
/**
* parse float保留两位小数四舍五入
* 空格或者非数字认为是0
2021-11-05 09:01:26 +00:00
* @param x
* @returns {*}
*/
function f2(x) {
if(!x){
return 0;
2021-11-05 09:01:26 +00:00
}
var f = parseFloat(x);
if (isNaN(f)) {
return 0;
2021-11-05 09:01:26 +00:00
}
return Math.round(x*100)/100;
}
2021-11-08 06:41:33 +00:00
/**
* 计算利润率
* @param r1
* @param r2
* @returns {*}
*/
function rate(r1,r2) {
if(!r1 || !r2 || r1==0 || r2==0){
return 0;
}
return f2(r1*100/r2);
}
2021-11-18 02:36:12 +00:00
/**
* 保留两位小数
*/
function bindNumberInput() {
//所有的数字输入框
var $inputs = $("input[type='number']");
//键盘键弹起的时候
$inputs.keyup(function () {
var value = $(this).val();
value = value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
if (value.indexOf(".") < 0 && value != "") {
//以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额
value = parseFloat(value);
}
$(this).val(value);
});
//失去焦点时再校验一遍
$inputs.blur(function() {
var value = $(this).val();
2021-11-23 11:31:10 +00:00
var reg = new RegExp("^[0-9]+(.[0-9]{1,2})?$");
2021-11-18 02:36:12 +00:00
if (reg.test(value)) {
2021-11-23 11:31:10 +00:00
//是数字就是数字,可以两位或者不带小数
$(this).val(value);
2021-11-18 02:36:12 +00:00
} else {
$(this).val("");
}
});
}
2021-11-23 11:31:10 +00:00
/**
* 保留整数
*/
function integerNumber(input) {
input.value=input.value.replace(/[^\d]/g,'').replace(/^0{1,}/g,'');
}
2021-11-18 02:36:12 +00:00
2021-11-08 06:41:33 +00:00
function postAjax(url, data, callback) {
2021-11-03 09:53:27 +00:00
$.ajax({
url: url,
data: JSON.stringify(data),
type: "post",
dataType: "json",
contentType:"application/json",
async: false,
success: function (d) {
console.log(d);
2021-11-18 11:25:06 +00:00
if(callback) {
callback(data, d);
}
}
});
}
function getAjax(url, params, callback) {
$.ajax({
url: url,
data: params,
type: "get",
dataType: "json",
/*contentType:"application/json",*/
async: false,
success: function (d) {
console.log(d);
if(callback) {
callback(params, d);
}
2021-11-03 09:53:27 +00:00
}
});
}
2021-11-17 08:45:37 +00:00
2021-11-17 09:28:55 +00:00
/**
* 获取给定名字的input框的值
* @param name
* @returns {*|jQuery}
*/
function inputVal(name) {
return $("input[name='"+name+"']").val();
}
2021-11-17 08:45:37 +00:00
2021-11-17 09:28:55 +00:00
/**
* 获取给定class名字的input框的值
* @param className
* @returns {*|jQuery}
*/
function classVal(className) {
return $("."+className).val();
}
2021-11-17 08:45:37 +00:00
/**
* 统计成本(含税)有一项没填就置空
*/
function calCostInclude() {
2021-11-17 09:28:55 +00:00
var costPurchaseDeviceTaxInclude = inputVal("costPurchaseDeviceTaxInclude");
var costPurchaseBuildTaxInclude = inputVal("costPurchaseBuildTaxInclude");
var costPurchaseServiceTaxInclude = inputVal("costPurchaseServiceTaxInclude");
var costPurchaseOtherTaxInclude = inputVal("costPurchaseOtherTaxInclude");
var costProjectManageTaxInclude = inputVal("costProjectManageTaxInclude");
var costOtherOtherTaxInclude = inputVal("costOtherOtherTaxInclude");
2021-11-17 08:45:37 +00:00
2021-11-17 08:47:49 +00:00
var $costTotalTaxInclude = $("input[name='costTotalTaxInclude']");
2021-11-17 08:45:37 +00:00
2021-11-22 02:32:36 +00:00
$costTotalTaxInclude.val(f2(costPurchaseDeviceTaxInclude) +f2(costPurchaseBuildTaxInclude)
+f2(costPurchaseServiceTaxInclude)+f2(costPurchaseOtherTaxInclude)
+f2(costProjectManageTaxInclude)+f2(costOtherOtherTaxInclude));
2021-11-17 08:45:37 +00:00
}
/**
* 统计成本(不含税)有一项没填就置空
*/
function calCostExclude() {
2021-11-17 09:28:55 +00:00
var costPurchaseDeviceTaxExclude = inputVal("costPurchaseDeviceTaxExclude");
var costPurchaseBuildTaxExclude = inputVal("costPurchaseBuildTaxExclude");
var costPurchaseServiceTaxExclude = inputVal("costPurchaseServiceTaxExclude");
var costPurchaseOtherTaxExclude = inputVal("costPurchaseOtherTaxExclude");
var costProjectManageTaxExclude = inputVal("costProjectManageTaxExclude");
var costOtherOtherTaxExclude = inputVal("costOtherOtherTaxExclude");
2021-11-17 08:45:37 +00:00
2021-11-17 08:47:49 +00:00
var $costTotalTaxExclude = $("input[name='costTotalTaxExclude']");
2021-11-17 08:45:37 +00:00
2021-11-22 02:32:36 +00:00
$costTotalTaxExclude.val(f2(costPurchaseDeviceTaxExclude)+f2(costPurchaseBuildTaxExclude)
+f2(costPurchaseServiceTaxExclude)+f2(costPurchaseOtherTaxExclude)
+f2(costProjectManageTaxExclude)+f2(costOtherOtherTaxExclude));
2021-11-17 08:45:37 +00:00
}