简体   繁体   English

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

[英]Get value if td contains specific text in jquery

I have below table with all the leaves of employee at a certain column. 我在下面的表格中列出了某一列的员工的所有叶子。

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

Using jquery or just core javascript, how do I get the number of days for the "Vacation Leave" and number of days for the "Sick Leave" if the were approved. 使用jquery或核心javascript,如果获得批准,我如何获得“休假”的天数和“病假”的天数。

This is how I fetch the table 这是我获取表格的方式

    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;
  }

Example: 例:

Vacation Leave = 2 Sick Leave = 1 休假= 2病假= 1

Thank you. 谢谢。

Add an id to your table like <table id="leaveTable"> then you could use something like this: <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);

It can be done by keeping two variables ( countSickLeave and countVacationLeave ) and incrementing them in for loop for respective type of leave. 它可以通过保留两个变量( countSickLeavecountVacationLeave )并将它们在for循环中递增以用于相应类型的假。


See the modification bellow. 请参见下面的修改。

 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; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM