禁用表中的行单击

Disable row click in table

本文关键字:单击      更新时间:2023-09-26

我想根据列中设置的标志值禁用行点击其余的行应该可以点击,也可以在某些列之间点击

具有true的前两行应该是不可擦除的。但是,其余的行应该可以从第1-5列中单击。

这是链接

提取行中最后一个单元格的.filter并测试它:

$('#table tr').find('td:gt(0):lt(5)').on("click", function() {
    if ($(this).closest('tr').find('td:eq(5)').text() != "true") {
        alert("hello");
    }
})​;

http://jsfiddle.net/mblase75/mEbUG/2/

或者,您可以在调用CCD_4:之前.on("click")清除不需要的单元格

$('#table tr').find('td:gt(0):lt(5)').filter(function() {
    return ($(this).closest('tr').find('td:eq(5)').text() != "true");
}).on('click', function() {
    alert("hello");
});​

http://jsfiddle.net/mblase75/mEbUG/12/

如果我理解正确的话。你只想让没有"true"单元格的行可以点击吗?还是希望每个单元格都可以点击,但不希望带有"true"的单元格可以点击?

这里有一个例子,只有在没有包含单词"true"的单元格的情况下,行才可以点击:

var hasTrueCell = function(cells) {
    for (var x = 0; x < cells.length; x++) {
        if (cells[x].innerHTML == "true")
            return true;
    }        
    return false;
}
var table = document.getElementById("table");
var rows = table.getElementsByTagName("TR");
for (var x = 0; x < rows.length; x++) {
    if (!hasTrueCell(rows[x].childNodes))
        rows[x].onclick = function() {         
            alert("hello");    
        }        
}

编辑(评论后)

现在,这应该奏效了。

$('#table tr').filter(function() {
    return $(this).find('td:contains("true")').length === 0;
}).click(function() {
    alert("hello");
});​​  

​演示

参考

$('#table tr').find('td:gt(0):lt(5)').click(function(){if($(this).text()!="true")alert(1)})​