【发布时间】:2014-02-20 20:45:44
【问题描述】:
单击同一表格内的超链接时,我需要禁用表格单元格内的所有复选框。
我正在使用以下 jquery 代码来选择嵌套在表格中的所有复选框。
$el = $(this).parents('table:eq(0)')[0].children('input[type="checkbox"]');
$($el).attr('checked', true);
由于某种原因,这段代码无法正常工作。
谁能告诉我如何解决它?
【问题讨论】:
单击同一表格内的超链接时,我需要禁用表格单元格内的所有复选框。
我正在使用以下 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');
【讨论】:
另请参阅:selector/checkbox
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');
}
});
【讨论】:
这是我的解决方案
// 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);
});
});
【讨论】:
------------------- 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>
【讨论】: