示例:
HTML:
<form>
<table>
<tr>
<td><input type="checkbox" id="select_all"/></td>
</tr>
<tr>
<td><input type="checkbox" name="select[]"/></td>
</tr>
<tr>
<td><input type="checkbox" name="select[]"/></td>
</tr>
<tr>
<td><input type="checkbox" name="select[]"/></td>
</tr>
</table>
</form>
jQuery:
//JQuery 1.6版本以前
$('#select_all').change(function() {
var checkboxes = $(input[type='checkbox']);
if($(this).is(':checked')) {
checkboxes.attr('checked', 'checked');
} else {
checkboxes.removeAttr('checked');
}
});
//或者
$('#select_all').change(function() {
var checkboxes = $(input[type='checkbox']);
if($(this).is(':checked')) {
checkboxes.attr('checked', true);
} else {
checkboxes.attr('checked', false);
}
});
//JQuery 1.6版本以后
$(function() {
$('#select_all').change(function(){
var checkboxes = $(input[type='checkbox']);
if($(this).prop('checked') == true){
checkboxes.prop('checked',true);
} else{
checkboxes.prop('checked',false);
}
});
});