2017-06-14 67 views
-3

如果我有一列3个复选框的onclick任何复选框,我想设置复选框的值在整列(未行)jQuery的:设置复选框的值

如果复选框中的第2列选中/取消选中,我想要选中/取消选中同一列(第二列)中的所有其他复选框。

这就是我想实现的代码::

var a =$j('#table tr').length; 
$j("input[type='checkbox']").change(function() { 
    var ch = this.checked; 
    if checked checkbox is in 2nd column , all the checkboxes in the 2nd column(td) should be checked (row length varies not static) 
}); 
+1

你能提供代码样本和您正在使用的jQuery的版本吗? – admcfajn

+0

这应该有所帮助:https://stackoverflow.com/questions/15330765/how-to-check-all-checkboxes-of-parents-siblings-on-click-with-jquery – admcfajn

+0

请[编辑]你的问题,以显示您的HTML样本。这种行为可以通过jQuery很多方式轻松实现,因此以HTML为出发点可以让人们提出最适合您的结构的方式。 – nnnnnn

回答

1

您可以使用.index() method来确定哪个列被点击。

在点击处理程序中,this是点击复选框,因此$(this).closest("td").index()会为您提供其行中包含td的从零开始的索引。

然后,您可以使用nth-child() selector(从一个,而不是零)来获取该列中的所有项目。

你没有显示你的HTML,所以我编写的基于一个非常简单的<table>结构,你可以看到以下内容:

var table = $("table").on("click", ":checkbox", function() { 
 
    var column = $(this).closest("td").index() + 1 
 
    table.find("td:nth-child(" + column + ") :checkbox").prop("checked", this.checked) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<table> 
 
    <tr><td><input type="checkbox"></td><td><input type="checkbox"></td><td><input type="checkbox"></td><td><input type="checkbox"></td></tr> 
 
    <tr><td><input type="checkbox"></td><td><input type="checkbox"></td><td><input type="checkbox"></td><td><input type="checkbox"></td></tr> 
 
    <tr><td><input type="checkbox"></td><td><input type="checkbox"></td><td><input type="checkbox"></td><td><input type="checkbox"></td></tr> 
 
    <tr><td><input type="checkbox"></td><td><input type="checkbox"></td><td><input type="checkbox"></td><td><input type="checkbox"></td></tr> 
 
</table>

+0

我已经能够达到共享的效果了:但是我想只在复选框的行包含特定文本值时才更改复选框值。我尝试了下面的代码。但失败: var table = $ j( “#table tr td:contains('Test')”)。on(“click”,“:checkbox”,function(){var column = $ j(this).closest(“td”)。index() + 1; table.find(“td:nth-​​child(”+ column +“):checkbox”)。prop(“checked”,this.checked); }); – MGD

1

如果我理解正确的话,你想要的复选框要么检查或在同一时间选中的所有3。这应该适合你。

$(document).on('click', '.class1, .class2, .class3', function(){ 
    var checked = $(this).prop('checked'); 
    if(checked === true) { 
     $('.class1, .class2, .class3').prop('checked', true); 
    } 
    else { 
     $('.class1, .class2, .class3').prop('checked', false); 
    } 
}); 

只要记住要将'.class1,.class2,.class3'更改为您正在使用的任何类。

+1

'this.checked'比'$(this).prop('checked')'更容易阅读,输入速度更快,执行速度更快。你可以用一行代替整个函数:$('。class1,.class2,.class3')。prop('checked',this.checked);'。但无论如何,这个答案不会将CB更新限制在当前列的复选框中。 – nnnnnn

+1

所有非常有效的点,谢谢! – ottiem

+0

这是我试图实现的代码 var a = $ j('#table tr')。length; $ j(“input [type ='checkbox']”)。change(function(){ \t var ch = this.checked; \t如果选中复选框位于第二列,则第二列中的所有复选框应为检查(行长度不固定) }); – MGD

0

按照jQuery的文档,根据您的版本,这可能需要调整才能正确使用。 https://api.jquery.com/prop/

<div class="column">check one: 
    <input type="checkbox" value="a"> 
    <input type="checkbox" value="b"> 
    <input type="checkbox" value="c"> 
</div> 
<script> 
$("input[type='checkbox']").click(function(){ 
    var checked = $(this).prop("checked"); 
    // console.log(checked,typeof checked); 
    if(checked === true){ 
    $(this).siblings("input[type='checkbox']").prop("checked",true); 
    }else{ 
    $(this).siblings("input[type='checkbox']").prop("checked",false); 
    } 
}); 
</script> 

我用.siblings这里,但还有许多其他的方式来做到这一点。

编辑:从@ottiem

$("input[type='checkbox']").click(function(){ 
    $(this).siblings("input[type='checkbox']").prop("checked",this.checked); 
}); 

note使用输入的是this.checked上面使用的JavaScript的this,没有jQuery的$(this)