2015-12-22 51 views
0

我使用以下jQuery在第一列中選​​擇一個複選框,當我們點擊一​​行時,除了當我們點擊一​​行中的最後一個單元時。jquery - 不選擇基於th類的td

tableid.on('click', 'tbody td:not(td:last-child)', function (e) { 
    $(this).parent().find('input[type="checkbox"]').trigger('click'); 
}); 

我加了noselecting class to thead> th。現在像我跳過最後一個單元格使用:not(td:last-child')我怎樣才能跳過td從點擊欄目thead> th與類noselecting

+0

我認爲你正在嘗試做的[這](http://stackoverflow.com/questions/18296850/ withing單擊處理點擊錶行到使用,jQuery的選擇 - 複選框 - )? –

+0

我想選擇一個tbody> td,如果thead> th.noselecting存在。 – karmendra

回答

0

您可以測試它使用索引

var tableid = $('#mytable'); 
 
tableid.on('click', 'tbody td:not(td:last-child)', function(e) { 
 
    var $this = $(this), 
 
    index = $this.index(); 
 
    if (!$this.closest('table').find('th').eq(index).hasClass('noselecting')) { 
 
    $this.parent().find('input[type="checkbox"]').trigger('click'); 
 
    } 
 
});
td, 
 
th { 
 
    border: 1px solid grey; 
 
} 
 
.noselecting { 
 
    background-color: lightblue; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="mytable"> 
 
    <thead> 
 
    <th>1</th> 
 
    <th class="noselecting">2</th> 
 
    <th>3</th> 
 
    <th>4</th> 
 
    <th class="noselecting">5</th> 
 
    </thead> 
 
    <tbody> 
 
    <td>1</td> 
 
    <td> 
 
     <input type="checkbox" /> 
 
    </td> 
 
    <td>1</td> 
 
    <td>1</td> 
 
    <td>1</td> 
 
    </tbody> 
 
</table>