As the title suggests, I want to be able to click on a table td OR the checkbox itself to toggle a checkbox on or off. The solution below works when clicking on the td, but for some reason when you click on the checkbox nothing happens. Any ideas as to what is going on here?
$('td').click(function (e) {
// Toggle checkbox
$('input:checkbox', this).prop('checked', function (i, value) {
return !value;
});
});
Thanks!
解决方案
It's because the checkbox is inside the
and you have attached click event to theto overcome this, Check for the target type and if it's not input checkbox, you need to process it.
$('td').click(function (event) {
if (!$(event.target).is('input')) {
$('input:checkbox', this).prop('checked', function (i, value) {
return !value;
});
}
});
refer