/** * parse float保留两位小数,四舍五入 * 空格或者非数字认为是0 * @param x * @returns {*} */ function f2(x) { if(!x){ return 0; } var f = parseFloat(x); if (isNaN(f)) { return 0; } return Math.round(x*100)/100; } /** * 计算利润率 * @param r1 * @param r2 * @returns {*} */ function rate(r1,r2) { if(!r1 || !r2 || r1==0 || r2==0){ return 0; } return f2(r1*100/r2); } function postAjax(url, data, callback) { $.ajax({ url: url, data: JSON.stringify(data), type: "post", dataType: "json", contentType:"application/json", async: false, success: function (d) { console.log(d); callback(data, d); } }); } /** * 获取给定名字的input框的值 * @param name * @returns {*|jQuery} */ function inputVal(name) { return $("input[name='"+name+"']").val(); } /** * 获取给定class名字的input框的值 * @param className * @returns {*|jQuery} */ function classVal(className) { return $("."+className).val(); } /** * 统计成本(含税),有一项没填就置空 */ function calCostInclude() { var costPurchaseDeviceTaxInclude = inputVal("costPurchaseDeviceTaxInclude"); var costPurchaseBuildTaxInclude = inputVal("costPurchaseBuildTaxInclude"); var costPurchaseServiceTaxInclude = inputVal("costPurchaseServiceTaxInclude"); var costPurchaseOtherTaxInclude = inputVal("costPurchaseOtherTaxInclude"); var costProjectManageTaxInclude = inputVal("costProjectManageTaxInclude"); var costOtherOtherTaxInclude = inputVal("costOtherOtherTaxInclude"); var $costTotalTaxInclude = $("input[name='costTotalTaxInclude']"); if(costPurchaseDeviceTaxInclude && costPurchaseBuildTaxInclude && costPurchaseServiceTaxInclude && costPurchaseOtherTaxInclude && costProjectManageTaxInclude && costOtherOtherTaxInclude){ $costTotalTaxInclude.val(f2(costPurchaseDeviceTaxInclude) +f2(costPurchaseBuildTaxInclude) +f2(costPurchaseServiceTaxInclude)+f2(costPurchaseOtherTaxInclude) +f2(costProjectManageTaxInclude)+f2(costOtherOtherTaxInclude)); }else { $costTotalTaxInclude.val(""); } } /** * 统计成本(不含税),有一项没填就置空 */ function calCostExclude() { var costPurchaseDeviceTaxExclude = inputVal("costPurchaseDeviceTaxExclude"); var costPurchaseBuildTaxExclude = inputVal("costPurchaseBuildTaxExclude"); var costPurchaseServiceTaxExclude = inputVal("costPurchaseServiceTaxExclude"); var costPurchaseOtherTaxExclude = inputVal("costPurchaseOtherTaxExclude"); var costProjectManageTaxExclude = inputVal("costProjectManageTaxExclude"); var costOtherOtherTaxExclude = inputVal("costOtherOtherTaxExclude"); var $costTotalTaxExclude = $("input[name='costTotalTaxExclude']"); if(costPurchaseDeviceTaxExclude && costPurchaseBuildTaxExclude && costPurchaseServiceTaxExclude && costPurchaseOtherTaxExclude && costProjectManageTaxExclude && costOtherOtherTaxExclude){ $costTotalTaxExclude.val(f2(costPurchaseDeviceTaxExclude)+f2(costPurchaseBuildTaxExclude) +f2(costPurchaseServiceTaxExclude)+f2(costPurchaseOtherTaxExclude) +f2(costProjectManageTaxExclude)+f2(costOtherOtherTaxExclude)); }else { $costTotalTaxExclude.val(""); } }