当单击表格内的超链接时,我需要禁用表格单元格内的所有复选框。
我正在使用以下 jQuery 代码来选择嵌套在表格中的所有复选框。
$el = $(this).parents('table:eq(0)')[0].children('input[type="checkbox"]');
$($el).attr('checked', true);
由于某些原因,这段代码不起作用。
有人能给我展示如何修复它吗?
$('table input[type=checkbox]').attr('disabled','true');
如果你有一张表的ID
$('table#ID input[type=checkbox]').attr('disabled','true');
禁用?
$("a.clickme").click(function(){
$(this) // Link has been clicked
.closest("td") // Get Parent TD
.find("input:checkbox") // Find all checkboxes
.attr("disabled", true); // Disable them
});
$("a.clickme").click(function(){
$(this) // Link has been clicked
.closest("td") // Get Parent TD
.find("input:checkbox") // Find all checkboxes
.attr("checked", false); // Uncheck them
});
你的代码可以更加简洁:
$el = $(this).parents('table:eq(0)')[0].children('input[type="checkbox"]');
可能是:
$el = $(this).parents('table:first :checkbox');
$el.attr('disabled', 'disabled');
或者检查它们:
$el.attr('checked', 'checked');
或者取消勾选它们:
$el.removeAttr('checked');
$el.removeAttr('disabled');
另请参阅:选择器/复选框
jQuery("#hyperlink").click(function() {
jQuery('#table input:checkbox').attr('disabled', true);
return false;
});
// 启用/禁用所有复选框
$('#checkbox').click(function() {
var checked = $(this).attr('checked');
var checkboxes = '.checkboxes input[type=checkbox]';
if (checked) {
$(this).attr('checked','checked');
$(checkboxes).attr('disabled','true');
} else {
$(this).removeAttr('checked');
$(checkboxes).removeAttr('disabled');
}
});
------------------------------- 下面是 HTML 代码 ------------------------------
<table id="myTable">
<tr>
<td><input type="checkbox" checked="checked" /></td>
<td><input type="checkbox" checked="checked" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
</table>
<input type="button" onclick="callFunction()" value="Click" />
-------------------------------以下是JQuery代码-------------------------------
<script type="text/javascript">
function callFunction() {
//:
$('table input[type=checkbox]').attr('disabled', 'true');
}
</script>
这是我的解决方案
// Action sur le checkbox
$("#tabEmployes thead tr th:first input:checkbox").click(function() {
var checked = $(this).prop('checked');
$("#tabEmployes tbody tr td:first-child input:checkbox").each(function() {
$(this).prop('checked',checked);
});
});