2017-04-25 106 views
-1

我需要在html表中找到在td中單擊的相應複選框的表頭上的類。實際上有兩個標題行存在。請幫助我編寫js代碼。在jQuery中點擊複選框的表頭上的類找到

<table runat="server" border="1" width="100%"> 
    <tr ><th colspan="4">Aetna cOmmercial</th></tr> 
     <tr class="row0"> 
     <th class=colindex0 >Alta</th> 
     <th class=colindex1>Hplan</th> 
     <th class=colindex2 >Hosp</th> 
     <th class=colindex3 >Other</th> 

     </tr> 
     <tr class="row1"> 
     <td class="gvclass1"> 
      <asp:CheckBox ID="CheckBox1" runat="server" Checked="true" /></td> 
     <td class="gvclass1"> 
      <asp:CheckBox ID="CheckBox2" runat="server" /></td> 
     <td class="gvclass1"> 
      <asp:CheckBox ID="CheckBox3" runat="server" /></td> 
     <td class="gvclass1"> 
      <asp:CheckBox ID="CheckBox4" runat="server" /></td> 
     <tr></table> 
+0

嗨,希望我們能幫上忙。到目前爲止你做了什麼?無論你嘗試過什麼, –

+0

都會成爲小提琴。 –

回答

0

指定每個複選框一個類,然後你可以找到最接近的TD指數和隨後以往TR和濾除由指數對應日。

注意:由於最後一個tr未正確關閉,因此您的html結構無效。

$(document).ready(function() { 
      $(".chk").click(function() { 
       //debugger; 
       var $td = $(this).closest('td'); 
       var $th = $td.closest('tr').prev('tr').find('> th:eq(' + $td.index() + ')'); 
       console.log($th.attr('class')); 
      }); 
}); 
+0

謝謝親愛的。它的工作... –

+0

這不工作的第二行在表.. –

0

這裏我調整了你的代碼,所以當你點擊任何複選框時,它會顯示th中的相應項目。 (你需要有jQuery)

<table runat="server" border="1" width="100%"> 
    <tr class="row0"> 
    <th class=colindex0>Alta</th> 
    <th class=colindex1>Hplan</th> 
    <th class=colindex2>Hosp</th> 
    <th class=colindex3>Other</th> 
    </tr> 
    <tr class="row1"> 
    <td class="gvclass1"><asp:CheckBox ID="CheckBox1" runat="server" Checked="true" /></td> 
    <td class="gvclass1"><asp:CheckBox ID="CheckBox2" runat="server" /></td> 
    <td class="gvclass1"><asp:CheckBox ID="CheckBox3" runat="server" /></td> 
    <td class="gvclass1"><asp:CheckBox ID="CheckBox4" runat="server" /></td> 
    <tr> 
</table> 

<script> 
    $('input').click(function(){ 
    var arr = $("input:checkbox"); 
    var arrCheked = []; 

    for (var i = 0; i < arr.length; i++) { 
     if (arr[i].checked){ 
     arrCheked.push($('th:nth-child(' + (i + 1) + ')').html()); 
     } 
    } 

    alert(arrCheked); 
    }); 
</script> 

我希望我幫了忙!