Deselect a checkbox
Im not sure why but for some reason when I click a td that has its checkbox already checked it does NOT deselect it.
$('table tr').click(function() {
checkBox = $(this).children('td').children('input[type=checkbox]');
if(checkBox.attr('checked'))
checkBox.attr('check开发者_JAVA百科ed', '');
else
checkBox.attr('checked', 'checked');
});
You want:
if(checkBox.attr('checked'))
checkBox.removeAttr('checked');
else
checkBox.attr('checked', 'checked');
If you're using jQuery v1.6, you should be using .prop()
if(checkBox.prop('checked'))
checkBox.prop('checked', false)
else
checkBox.prop('checked', true);
精彩评论