Javascript 如果td在jquery中包含特定文本,则获取值

Javascript 如果td在jquery中包含特定文本,则获取值,javascript,jquery,Javascript,Jquery,我在下表的某一列列出了员工的所有假期 Date Applied Leave Type Days From Date To Date Status 2017-01-01 Vacation Leave 1 2017-01-02 2017-01-03 No Action 2017-02-01 Sick Leave 1 2017-02-02 2017-02-03 Approved 2017-03-01 Vacation Le

我在下表的某一列列出了员工的所有假期

Date Applied  Leave Type      Days  From Date   To Date     Status
2017-01-01    Vacation Leave   1    2017-01-02  2017-01-03  No Action
2017-02-01    Sick Leave       1    2017-02-02  2017-02-03  Approved
2017-03-01    Vacation Leave   2    2017-03-02  2017-03-03  Approved
使用jquery或core javascript,如何获得批准后的“休假”天数和“病假”天数

我就是这样拿桌子的

    function writeResult(res){
    var p = document.getElementById("write_result");
    p.innerHTML = "";

    var addHtml = "<table align=\"center\" border=\"1\" cellspacing=\"0\" style=\"font-weight:regular;\">";
        addHtml += "<tr>"; 
        addHtml += "<th>Date Applied</th><th>Leave Type</th><th>Days</th><th>From Date</th><th>To Date</th><th>Status</th>";
        for(var i=0;i<res.length;i++){
          addHtml += "<tr>";
          addHtml += "<td>"+ changeDateFormat(res[i][0]) +"</td>";
          addHtml += "<td>"+ res[i][1] +"</td>";
          addHtml += "<td>"+ res[i][2] +"</td>";
          addHtml += "<td>"+ changeDateFormat(res[i][3]) +"</td>";
          addHtml += "<td>"+ changeDateFormat(res[i][4]) +"</td>";
          addHtml += "<td>"+ res[i][5] +"</td>";
          addHtml += "</tr>";
        }
        addHtml += "</tr>";
        addHtml += "</table>"
        addHtml += "<div>";
        addHtml += "<table>";
        addHtml += "<tr>";
        addHtml += "<th>Vacation Leaves: &nbsp;</th><td id='vacation_leaves'>0</td>";
        addHtml += "<th>&nbsp; &nbsp; Sick Leaves: &nbsp;</th><td id='sick_leaves'>1</td>";
        addHtml += "</tr>"
        addHtml += "</table>";
        addHtml += "</div>";
    p.innerHTML = addHtml;
  }
函数writeResult(res){
var p=document.getElementById(“写入结果”);
p、 innerHTML=“”;
var addHtml=“”;
addHtml+=“”;
addHtml+=“日期应用程序保存类型日期从日期到日期状态”;

对于(var i=0;i向表中添加一个id,如
,则可以使用以下内容:

var approvedVacLeave = 0;
var approvedSickLeave = 0;
$("#leaveTable tr").each(function (){
    var leaveType = $(this).find("td:nth-child(2)").text();
    var numDays = +$(this).find("td:nth-child(3)").text();
    var status = $(this).find("td:nth-child(6)").text();
    if (status == "Approved") {
        if (leaveType == "Vacation Leave") {
            approvedVacLeave += numDays;
        }
        else if (leaveType == "Sick Leave") {
            approvedSickLeave += numDays;
        }
    }
});
console.log("Vacation days = " + approvedVacLeave + " and sick days = " + approvedSickLeave);

这可以通过保留两个变量(
countSickLeave
countVacationLeave
)并在for循环中为各自的休假类型递增来实现。
请参见下面的修改

function writeResult(res){
var countSickLeave = 0; //count variable
var countVacationLeave = 0;
var p = document.getElementById("write_result");
p.innerHTML = "";

var addHtml = "<table align=\"center\" border=\"1\" cellspacing=\"0\" style=\"font-weight:regular;\">";
    addHtml += "<tr>"; 
    addHtml += "<th>Date Applied</th><th>Leave Type</th><th>Days</th><th>From Date</th><th>To Date</th><th>Status</th>";
    for(var i=0;i<res.length;i++){
      addHtml += "<tr>";
      addHtml += "<td>"+ changeDateFormat(res[i][0]) +"</td>";
      if(res[i][1] == "Sick Leave"){
          countSickLeave++; // increment the variable
      }
      if(res[i][2] == "Sick Leave"){
          countVacationLeave++;
      }
      addHtml += "<td>"+ res[i][1] +"</td>";
      addHtml += "<td>"+ res[i][2] +"</td>";
      addHtml += "<td>"+ changeDateFormat(res[i][3]) +"</td>";
      addHtml += "<td>"+ changeDateFormat(res[i][4]) +"</td>";
      addHtml += "<td>"+ res[i][5] +"</td>";
      addHtml += "</tr>";
    }
    addHtml += "</tr>";
    addHtml += "</table>"
    addHtml += "<div>";
    addHtml += "<table>";
    addHtml += "<tr>";
    // Append the variable count
    addHtml += "<th>Vacation Leaves: </th><td id='vacation_leaves'>" + countVacationLeave + "</td>";
    addHtml += "<th>Sick Leaves: </th><td id='sick_leaves'>" + countSickLeave + "</td>";
    addHtml += "</tr>"
    addHtml += "</table>";
    addHtml += "</div>";
p.innerHTML = addHtml;
}
函数writeResult(res){
var countSickLeave=0;//计数变量
var countVacationLeave=0;
var p=document.getElementById(“写入结果”);
p、 innerHTML=“”;
var addHtml=“”;
addHtml+=“”;
addHtml+=“日期应用程序保存类型日期从日期到日期状态”;

对于(var i=0;我可以将表格的实际原始html添加到您的问题中吗?您需要检查
Approved
tooThis外观,它也可以做我想要的事情,但我更愿意让另一个函数来做。谢谢。