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

时间:2017-02-06 04:46:11

标签: 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 Leave   2    2017-03-02  2017-03-03  Approved

使用jquery或核心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;
  }

示例:

休假= 2 病假= 1

谢谢。

2 个答案:

答案 0 :(得分:2)

<table id="leaveTable">中为您的表添加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);

答案 1 :(得分:1)

可以通过保留两个变量(countSickLeavecountVacationLeave)并在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;
}