想要获得祖父表td

时间:2016-07-01 11:35:17

标签: jquery

enter image description here 我有一个父表生成JSON加载到变量后。我希望当我点击类别列的“2A”然后它的值,即“2A”存储在一个变量然后相应的行的第二列,即列号,即19806存储在另一个变量中。

我的代码是

    <table width='99.6%'><tbody><tr>"+ "<td align='center' width='75%'><div id='detail_name'>Found "+trndata.total+" Trains between "+document.saform1.safrom.value+" and "+document.saform1.sato.value+" station</div></td>"+
"<td align='center' width='25%'><div id='div1_toolbar'><table style='font-size: 12px;' width='100%'><tbody><tr>"+
"</td><td align='center' class='cls_toolbar'> <a style='color: white; text-decoration: none;' href='#' onclick='javascript:h2i(); return false;' id='imglnk'>Save Image</a>"+
"</td><td align='center' class='cls_toolbar'> <a style='color: white; text-decoration: none;' href='#' onclick='javascript:h2p(); return false;' id='pdflnk'>Save PDF</a>"+
"</td><td align='center' class='cls_toolbar'> <a style='color: white; text-decoration: none;' href='#' onclick='javascript:window.print(); return false;' id='printlnk'>Print</a>"+
"</td></tr></tbody></table></div></td>"+
"</tr></tbody></table>"+
"<div class='datagrid'>"+
"<table><thead><tr>"+
"<th>S No</th><th>Train Number</th><th>Train Name</th><th>Source</th><th>Departure</th><th>Destination</th><th>Arrival</th><th>Travel Time</th><th>Day Run</th><th>Classes</th>"+
"</tr></thead>"+
"<tbody id='alstn'></tbody>"+
"<tfoot><tr><td colspan='10'><div id='no-paging'>Disclaimer: This application is for information purpose only. For latest updates refer to <a target='_blank' href='http://www.indianrail.gov.in'>Indian Railway</a> website. © Copyrights to <a target='_blank' href='http://digitzz.blogspot.com'>Digitzz</a> and it's author <a target='_blank' href='http://digitzz.blogspot.com/p/about.html'>Ravindra Gupta</a></div></tr></tfoot>"+
"</table></div>");

//fetching all station details
    for (var i = 0; i < trndata.total; i++) {    

   if (i%2 == 0)
    {
    $('#alstn').append("<tr><td>" + (i+1) + 
                       "</td><td>" + trndata.train[i].number + 
                       "</td><td>" + trndata.train[i].name +
                       "</td><td>" + trndata.train[i].from.name + 
                       "</td><td>" + trndata.train[i].src_departure_time +
                       "</td><td>" + trndata.train[i].to.name +
                       "</td><td>" + trndata.train[i].dest_arrival_time +
                       "</td><td>" + trndata.train[i].travel_time +
                       "</td><td>" + "<div width='auto' id=day"+(i)+"></div>" +
                       "</td><td>" + "<div width='auto'id=clss"+(i)+"></div>" +
                       "</td></tr>");

                      document.getElementById('day'+i).innerHTML = ("<table><tbody><tr id='dayadd"+i+"'></tr></tbody></table>");
                      for (var j = 0; j < trndata.train[i].days.length; j++) { 

                       if(trndata.train[i].days[j].runs == 'Y') {
                            if (trndata.train[i].days[j]['day-code'] == "MON")
                            {
                            $('#dayadd'+i).append("<td><span style='color:#009933;'>M</span></td>");
                            }
                            else if (trndata.train[i].days[j]['day-code'] == "TUE")
                            {
                            $('#dayadd'+i).append("<td><span style='color:#009933;'>T</span></td>");
                            }
                            else if (trndata.train[i].days[j]['day-code'] == "WED")
                            {
                            $('#dayadd'+i).append("<td><span style='color:#009933;'>W</span></td>");
                            }
                            else if (trndata.train[i].days[j]['day-code'] == "THU")
                            {
                            $('#dayadd'+i).append("<td><span style='color:#009933;'>T</span></td>");
                            }
                            else if (trndata.train[i].days[j]['day-code'] == "FRI")
                            {
                            $('#dayadd'+i).append("<td><span style='color:#009933;'>F</span></td>");
                            }
                            else if (trndata.train[i].days[j]['day-code'] == "SAT")
                            {
                            $('#dayadd'+i).append("<td><span style='color:#009933;'>S</span></td>");
                            }
                            else 
                            {
                            $('#dayadd'+i).append("<td><span style='color:#009933;'>S</span></td>");
                            }
                        }
                        else
                        {
                        $('#dayadd'+i).append("<td><span style='color:#ff0000;'>"+"X"+"</span></td>");                       
                        }
                      }         

                      document.getElementById('clss'+i).innerHTML = ("<table><tbody><tr id='classadd"+i+"'></tr></tbody></table>");
                      for (var k = 0; k < trndata.train[i].classes.length; k++) {

                       if(trndata.train[i].classes[k].available == 'Y') {
                       $('#classadd'+i).append("<td><b><a href='#' onclick='' id='daysids"+i+"'>"+trndata.train[i].classes[k]['class-code']+"</a></b></td>");
                       }
                       else
                       {
                       $('#classadd'+i).append("<td>"+"X"+"</td>");                       
                       }
                      }
     }

目前我正在使用

$(this).parent().parent().find('td.two').text();

但没有运气。这个功能我需要每行。

请帮助。

1 个答案:

答案 0 :(得分:1)

尝试与此相似的内容

在火车编号

中添加一个班级
<td class='train_number'>" + trndata.train[i].number +  "</td>.."
     $('body').on('click','[id^="daysids"]',function(){
        var daysids = $(this).text();
        var train_nr = $(this).closest('#alstn > tr').find('.train_number').text();
    });