JavaScript功能无法在Chrome& IE但适用于FireFox

时间:2014-10-06 09:02:00

标签: javascript php

最近我开发了具有如此多实时计算选项的系统。我在Firefox上测试了这些功能,但是当我尝试在 Chorme中运行这个程序时,它不起作用。它没有显示结果

function premiumCalculation(){

var shortPeriodRate = 100;
var spr             = document.getElementById("policyPeriod");
shortPeriodRate     = parseInt(spr.options[spr.selectedIndex].value);
var shortPeriodDate = spr.options[ spr.selectedIndex ].text;  
//================== hidden assign ======================================================
document.getElementById("policyPeriodInput").value = spr.options[spr.selectedIndex].text;
//=======================================================================================

var sumInsured  = document.getElementById("sumInsuredValue").value;
var basicRate   = document.getElementById("basicRate").value;
var srcc    = document.getElementById("srccRateInput").value;
var tc      = document.getElementById("tcRateInput").value;    

//============================= BASIC PREMIUM CALCULATION ==========================================
var shortPrecentage =  parseFloat(shortPeriodRate) / 100;
var basicPremium    = ((parseFloat(sumInsured) * parseFloat(basicRate)) * parseFloat(shortPrecentage)/100); 
if(basicPremium < 500){basicPremium = 500;}
document.getElementById("basicPremium").innerHTML   = money_format(basicPremium);
document.getElementById("basicPremiumInput").value  = basicPremium; 
//===================================================================================================


var currentDate = new Date();
var nextDate    = ''; 

var spd         = shortPeriodDate.split(" ");
var spdDate     = spd[0];

//================ FOR 3 & 10 DAYS ===============================
if(shortPeriodRate  == 5 || shortPeriodRate == 10){ 
    var month       = currentDate.getMonth()+ 1;
    var day         = currentDate.getDate() + parseFloat(spdDate);
    var year        = currentDate.getFullYear();
    if(month < 10){ month = "0" + month;}
    nextDate        = (year + '/' + month + '/' + day);
    nextDate        = new Date(nextDate);
    nextDate        = nextDate.toLocaleFormat('%Y-%m-%d'); 
    }
//================================================================
//================ FOR Months ==============================================
if(shortPeriodRate  != 5 && shortPeriodRate != 10 && shortPeriodRate != 25){ 
    var month       = (currentDate.getMonth()+ 1) +  parseFloat(spdDate);
    var day         = currentDate.getDate();
    var year        = currentDate.getFullYear();
    if(month < 10){ month = "0" + month;}
    nextDate        = (year + '/' + month + '/' + day); 
    nextDate        = new Date(nextDate);
    nextDate        = nextDate.toLocaleFormat('%Y-%m-%d');
    } 
//===========================================================================
//========================= DATE DIFFERENCE ==========================================
var oneDay      = 24*60*60*1000;    
var firstDate       = new Date(currentDate); 
var secondDate      = new Date(nextDate);   
var dateDifference  = Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)); 
dateDifference      = Math.round(dateDifference); 
//====================================================================================
//===================== SRCC PREMIUM CALCULATION ==============================================
var srccLogic       = document.getElementById('strikeRiot').value; //alert(srccLogic);
if(srccLogic == 'yes'){
var srccPrecentage  =  parseFloat(dateDifference) / 365; 
var srccPremium     =  (parseFloat(sumInsured) * parseFloat(srcc) * parseFloat(srccPrecentage)/100); 
document.getElementById("srccPremium").innerHTML        =   money_format(srccPremium);
document.getElementById("srccPremiumInput").value       =   srccPremium;
} else { 
srccPremium     = 0;
srccPrecentage  = 1;
document.getElementById("srccPremium").innerHTML        =   money_format(srccPremium);
document.getElementById("srccPremiumInput").value       =   srccPremium;
}
//=============================================================================================
//========================= TC PREMIUM CALCULATION ==========================================
var tcLogic         = document.getElementById('terrorism').value; //alert(srccLogic);
if(tcLogic == 'yes'){
var tcPrecentage    =  parseFloat(dateDifference) / 365; 
var tcPremium       =   (parseFloat(sumInsured) * parseFloat(tc) * parseFloat(tcPrecentage)/100); 
document.getElementById("tcPremium").innerHTML          =   money_format(tcPremium);
document.getElementById("tcPremiumInput").value         =   tcPremium;
} else { 
tcPremium       = 0;
tcPrecentage    = 1;
document.getElementById("tcPremium").innerHTML          =   money_format(tcPremium);
document.getElementById("tcPremiumInput").value         =   tcPremium;
}
//===========================================================================================
//==================== ADMIN FEE CALCULATION =======================================
    var adminFeeTotal       =  basicPremium +  srccPremium + tcPremium;
var adminFee        = (parseFloat(adminFeeTotal) * 0.3) / 100 ;
document.getElementById("adminFee").innerHTML           =   money_format(adminFee);
document.getElementById("adminFeeInput").value          =   adminFee;
//==================================================================================
//================== POLICY FEE======================================
document.getElementById("policyFee").innerHTML          =   "450.00";
document.getElementById("policyFeeInput").value         =   "450";
//===================================================================
//================== STAMPFEE CALCULATION ===========================
var totalUp         =   parseFloat(basicPremium) + parseFloat(srccPremium) + parseFloat(tcPremium) + parseFloat(adminFee) + parseFloat(450);
var stampFee        =   Math.round((totalUp * 0.1) / 100);
var stampFeeS       =   (totalUp * 0.1) / 100;
var roundUpString   =   stampFeeS.toString();
var roundUpVal      =   roundUpString.split(".");
var roundUpValT     =   roundUpVal[1]; var roundUpValTV =   parseFloat(roundUpValT);
//if(roundUpValTV > 0){ stampFee    =   stampFee + 1;}
document.getElementById("stampFee").innerHTML           =   money_format(stampFee);
document.getElementById("stampFeInput").value           =   stampFee;
//================================================================================
//================= NBT CALCULATION ==============================================
var nbtPremium      =   ((totalUp * 2) / 98); 
document.getElementById("nbt").innerHTML                =   money_format(nbtPremium);
document.getElementById("nbtInput").value               =   nbtPremium;
//================================================================================
//================ VAT CALCULATION ===============================================
var totalForVAT     =   parseFloat(totalUp) + parseFloat(nbtPremium); 
var VATPremium      =  ((totalForVAT * 12) / 100);
document.getElementById("vat").innerHTML                =   money_format(VATPremium);
document.getElementById("vatInput").value               =   VATPremium;
//================================================================================
//=============== GROSS CALCULATION ================
var grossPremium    =   parseFloat(totalForVAT) + parseFloat(stampFee);
document.getElementById("grossPremium").innerHTML       =   money_format(grossPremium);
document.getElementById("grossPremiumInput").value      =   grossPremium;
//==================================================
//=============== NETPREMIUM CALCULATION ===========
var netPremium      =   parseFloat(grossPremium) + parseFloat(VATPremium);
document.getElementById("netPremium").innerHTML         =   money_format(netPremium);
document.getElementById("netPremiumInput").value        =   netPremium;
//==================================================

}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:3)

根据您的开始主题中的注释,您可以简单地检测/重写缺少的函数:

Date.prototype.toLocaleFormat = Date.prototype.toLocaleFormat || function(pattern) {
    return pattern.replace(/%Y/g, this.getFullYear()).replace(/%m/g, (this.getMonth() + 1)).replace(/%d/g, this.getDate());
};

重写将全局可用,您可以使用此jsfiddle

进行测试