无法在if循环中调用Jquery函数

时间:2016-11-09 23:17:45

标签: javascript loops if-statement

我的第一个问题非常确定我在这里有点蠢,但我是初学者,非常感谢你的帮助。

我在一个网页上工作,其中有一个html表列出了几列数据。 当页面加载时,它会运行一个jquery脚本来计算不同类型的"事件"并将它们绘制在另一个表中,然后另一个jquery脚本填充图形。 我有第三个脚本(javascript),在单击按钮后,运行if循环,查看第一列中的数据,如果它与条件不匹配,则删除该行。

到目前为止一直很好,问题是我想要填充图表的脚本再次运行,但我不知道如何从if循环中调用它。

我把两个脚本放在下面,基本上我想在第二个脚本中调用第一个脚本。

$(function () {
    var NumFireAlarms = $("#incidents tr:contains('Fire Alarm')");
    $("#result").html(NumFireAlarms.length + " Fire Alarm");
    var firealarms = NumFireAlarms.length;
    document.getElementById("no_of_incident_type").rows[1].cells[1].innerHTML = firealarms

    var NumLockout = $("#incidents tr:contains('Lockout Out of Office Hours')");
    $("#result").html(NumLockout.length + " Lockout Out of Office Hours");
    var lockouts = NumLockout.length;
    document.getElementById("no_of_incident_type").rows[2].cells[1].innerHTML = lockouts

    var NumLockoutDayTime = $("#incidents tr:contains('Lockout Day Time')");
    $("#result").html(NumLockout.length + " Lockout Day Time");
    var lockoutsDayTime = NumLockoutDayTime.length;
    document.getElementById("no_of_incident_type").rows[3].cells[1].innerHTML = lockoutsDayTime

    var NumSensitiveIncident = $("#incidents tr:contains('Sensitive Incident')");
    $("#result").html(NumSensitiveIncident.length + " Sensitive Incident");
    var SensitiveIncident = NumSensitiveIncident.length;
    document.getElementById("no_of_incident_type").rows[4].cells[1].innerHTML = SensitiveIncident       
});



function filterForGraph() {

    var incident_category = document.getElementById("Incident_Category").value;
    var start_date = document.getElementById("start_date").value;
    var end_date = document.getElementById("end_date").value;
    var staff_type = document.getElementById("Job_Title").value;

    var i;
    var count = 0;
    var table_length = document.getElementById("incidents").rows;
    var TL = table_length.length;

    for (i = TL - 1; i >= 1; i--)
    {
        var category_column = document.getElementById("incidents").rows[i].cells.item(0).innerHTML;
        var date_column = document.getElementById("incidents").rows[i].cells.item(1).innerHTML;
        var staff_colunm = document.getElementById("incidents").rows[i].cells.item(8).innerHTML;

        if (category_column === incident_category)
        {
            alert("yay")
            count++
        }
        else if (category_column !== incident_category)
        {
            alert("boo")
            document.getElementById("incidents").deleteRow(i);
            //CALL FIRST SCRIPT HERE??

        }
    }

}

2 个答案:

答案 0 :(得分:0)

我删除了一些似乎没有做任何事情的代码,但我确定你可以把它们放回去。我想你可能想要这样的东西:

function updateTable(){
    var elResult = document.getElementById("result");
    var elNumIncidentType = document.getElementById("no_of_incident_type");

    var firealarms: document.querySelectorAll("#incidents tr:contains('Fire Alarm')").length;
    var lockouts: document.querySelectorAll("#incidents tr:contains('Lockout Out of Office Hours')").length;
    var lockoutsDayTime: document.querySelectorAll("#incidents tr:contains('Lockout Day Time')").length;
    var sensitiveIncident: document.querySelectorAll("#incidents tr:contains('Sensitive Incident')").length;

    elResult.innerHTML = "";
    elResult.innerHTML += "<div>" + firealarms + " Fire Alarm</div>";
    elResult.innerHTML += "<div>" + lockouts + " Lockout Out of Office Hours</div>";
    elResult.innerHTML += "<div>" + lockoutsDayTime + " Lockout Day Time</div>";
    elResult.innerHTML += "<div>" + sensitiveIncident + " Sensitive Incident</div>";

    elNumIncidentType.rows[1].cells[1].innerHTML = firealarms;
    elNumIncidentType.rows[2].cells[1].innerHTML = lockouts;
    elNumIncidentType.rows[3].cells[1].innerHTML = lockoutsDayTime;
    elNumIncidentType.rows[4].cells[1].innerHTML = sensitiveIncident;
}

function filterForGraph() {
    var elIncidents = document.getElementById("incidents");
    var incident_category = document.getElementById("Incident_Category").value;
    var table_length = document.getElementById("incidents").rows.length;

    for (var i = table_length - 1; i >= 1; i--) {
        var currentIncident = elIncidents.rows[i].cells;
        var category_column = currentIncident.item(0).innerHTML;
        if (category_column != incident_category) { elIncidents.deleteRow(i); }
    }

    updateTable();
}

$(function(){ updateTable(); });

答案 1 :(得分:0)

你好JonSG尝试了你的代码并且它没有确定原因,但它给了我一些想法,我想我已经破解了它

 function Populate_Incident_No_Table() {
    //previously function called updateTable
    $(function () {

        var NumFireAlarms = $("#incidents tr:contains('Fire Alarm')").length;
        document.getElementById("no_of_incident_type").rows[1].cells[1].innerHTML = NumFireAlarms

        var NumLockout = $("#incidents tr:contains('Lockout Out of Office Hours')").length;
        document.getElementById("no_of_incident_type").rows[2].cells[1].innerHTML = NumLockout

        var NumLockoutDayTime = $("#incidents tr:contains('Lockout Day Time')").length;
        document.getElementById("no_of_incident_type").rows[3].cells[1].innerHTML = NumLockoutDayTime

        var NumSensitiveIncident = $("#incidents tr:contains('Sensitive Incident')").length;
        document.getElementById("no_of_incident_type").rows[4].cells[1].innerHTML = NumSensitiveIncident
    });
}

function filterForGraph(){

    var incident_category = document.getElementById("Incident_Category").value;
    var i;
    var TL = document.getElementById("incidents").rows.length;

    for (i = TL - 1; i >= 1; i--)
    {
        var category_column = document.getElementById("incidents").rows[i].cells.item(0).innerHTML;

        if (category_column !== incident_category)
        {

            document.getElementById("incidents").deleteRow(i);

        }
    }
    Populate_Incident_No_Table();
    drawGraph();
}

我认为问题是我试图调用这些函数的方式。所以我为实现我想要的而做了什么(请原谅任何不好的术语/措辞)。

首先,我尝试将函数命名为$(函数updateTable(){当我尝试调用此函数时,这不起作用,如此updateTable();

我尝试的第二件事是将updateTable()函数&#34;放在&#34;一个函数并调用该函数。这对我有用,我不知道为什么。

感谢你的帮助,没有它,我不会想到尝试我做的事情