试图跟踪调用java脚本函数的次数

时间:2015-10-25 18:39:30

标签: javascript html

var totalEnteredCount = 0;
function totalEntered(field){
if(field.value != '')
{
    totalEnteredCount++;
}
alert(Count);
if(totalEnteredCount == 3)
{
    var IV = document.getElementById("IVUnit").value;
    var FV = document.getElementById("FVUnit").value;
    var Distance = document.getElementById("DUnit").value;
    var Acceleration = document.getElementById("AUnit").value;
    var Time = document.getElementById("TUnit").value;
}
}

每次文本框输入或不输入数据时,此函数都会从HTML调用onBlur。如果输入数据,我希望它将totalEnteredCount增加1.但是全局变量是未定义的。有没有办法跟踪调用函数的次数?

HTML如下:

<td>Initial Velocity: </td>
       <td> <input type = "textbox" name ="InitVelocityInput" onKeyPress="return isAcceptable(event)" onBlur = "totalEntered(this)" id = "IVUnit"> </td>

2 个答案:

答案 0 :(得分:1)

您正在尝试提醒未定义的“计数”:alert(Count); 这会导致运行时错误并阻止其余代码执行。尝试评论这一行

答案 1 :(得分:1)

更正代码 -

var totalEnteredCount = 0;
function totalEntered(field)
{
    if(field.value !== '') //using !== instead of != for string comparision
    {
        totalEnteredCount++;
    }
    alert(totalEnteredCount); //using totalEnteredCount instead of Count
    if(totalEnteredCount == 3)
    {
        var IV = document.getElementById("IVUnit").value;
        var FV = document.getElementById("FVUnit").value;
        var Distance = document.getElementById("DUnit").value;
        var Acceleration = document.getElementById("AUnit").value;
        var Time = document.getElementById("TUnit").value;
    }
}

JS Bin: http://jsbin.com/xujugebeva/edit?html,js,output