2012-07-05 89 views
2

如何用jQuery可以做出选择所有全部不选我,所以我可以选择每个单独的一个,但有超过20复选框的,我需要选择全部或取消选择所有jQuery的检查所有使用肘法

<input type="checkbox" name="filters" rel="2" checked="checked" /> name 
&emsp; <input type="checkbox" name="filters" rel="3" checked="checked" />tname 
&emsp;<input type="checkbox" name="filters" rel="4" checked="checked" />lname 

JQUERY

$(function(){ 
    $(':checkbox').on('change', function(){ 
     $('td').filter(':nth-child(' + $(this).attr('rel') + ')').toggle(); 
     $('th').filter(':nth-child(' + $(this).attr('rel') + ')').toggle(); 
    }); 
}) 

每一个会隐藏在这个时候一个字段,但将是检查所有checkbutton不错。

感谢

+0

查看我的回答[此问题](http://stackoverflow.com/questions/11352287/check-all-function-when-using-ryan-faits-custom-form-elements/11352400#11352400),发布了一个几分钟前。 – elclanrs 2012-07-05 21:41:14

回答

2

使用prop()功能(jQuery的V1.6 +):

要检查所有:

$("input[type='checkbox']").prop("checked", true); 

要取消所有:

$("input[type='checkbox']").prop("checked", false); 

要检查一个输入被检查,你可以使用if (this.checked)

+0

Zee,使用此功能可以检查和取消选中所有复选框,但不会切换表格中的字段。每个复选框将隐藏一个表格行。有没有办法将检查全部纳入此项? – user1497166 2012-07-05 22:04:21

+0

而不是在'change'事件中使用'toggle'使用'if(this.checked)'。 – 2012-07-05 22:10:32