乘法变量不警报

时间:2011-05-24 11:05:40

标签: javascript

我有一个脚本从输入字段调用变量值并将它们相乘, 在我的功能没有执行的那一刻,我也没有得到警报,我认为这是因为我的if声明,任何人都可以看到出错了吗?

function Calculate() {

    var ContentMinutes = document.getElementById ("ContentMinutes").value;
    var ContentMinutesSelect = document.getElementById('ContentMinutesDD')
        .options[document.getElementById('ContentMinutesDD').selectedIndex].value

    if (ContentMinutesSelect == 0.0166)
    {
        var RenderingHours = 10;
        var VideoHours = 5;
        var VideoSeconds = 1;
        document.getElementById("RenderHours").innerHTML=RenderingHours;
        document.getElementById("VideoHours").innerHTML=VideoHours;
        document.getElementById("VideoSeconds").innerHTML=VideoSeconds;
    }
    else if (ContentMinutesSelect == 0.0003)
    {
        var RenderingHours = 1540;
        var VideoHours = 54;
        var VideoSeconds = 1;
        document.getElementById("RenderHours").innerHTML=RenderingHours;
        document.getElementById("VideoHours").innerHTML=VideoHours;
        document.getElementById("VideoSeconds").innerHTML=VideoSeconds;
    }
    else
    {
        var RenderingHours = 6410;
        var VideoHours = 345;
        var VideoSeconds = 124;
        document.getElementById("RenderHours").innerHTML=RenderingHours;
        document.getElementById("VideoHours").innerHTML=VideoHours;
        document.getElementById("VideoSeconds").innerHTML=VideoSeconds;
    }


    var NoOfFrames = document.getElementById ("NoOfFrames").value;
    //var EstimatedCoreHours = document.getElementById ("EstimatedCoreHours").value;
    var ServiceLevel = document.getElementById('SerivceLevelDD')
        .options[document.getElementById('SerivceLevelDD').selectedIndex].value;
    var RenderHours = 1;
    var CoresInTest = document.getElementById ("CoresInTest").value;

    var EstimatedCoreHours =  GetNumeric(NoOfFrames) 
        * GetNumeric(RenderingHours) 
        * GetNumeric(CoresInTest);

    var EstimatedTotal =  GetNumeric(ServiceLevel) 
        * GetNumeric(EstimatedCoreHours);

    alert('Estimated Cost = '
        +EstimatedTotal.toFixed(2)
        + 'Estimated Core Hours = '
        +EstimatedCoreHours);

    document.getElementById("EstimatedCoreHours").innerHTML = 
         EstimatedCoreHours.toFixed(2);
    document.getElementById("EstimatedTotal").innerHTML = 
         EstimatedTotal.toFixed(2);

    document.getElementById("EstimatedCoreHours").style.backgroundColor="yellow";
    document.getElementById("EstimatedTotal").style.backgroundColor="yellow";
}

function GetNumeric(val) {

    if (isNaN(parseFloat(val))) {
        return 0;
    }
    return parseFloat(val);
}

2 个答案:

答案 0 :(得分:0)

if (ContentMinutesSelect == 0.0166)我认为当你执行.value时,你会得到字符串结果。

所以你的比较应该是

if (ContentMinutesSelect == "0.0166")

答案 1 :(得分:0)

如果代码中的任何行导致错误,则代码将不显示警告,例如,如果文档中没有id为“ContentMinutes”的元素。调试的最佳方法是使用类似firebug的东西,或者你总是可以输入一堆警报并找出问题所在。