Answers:
略微更改您的标记:
$(function() {
enable_cb();
$("#group1").click(enable_cb);
});
function enable_cb() {
if (this.checked) {
$("input.group1").removeAttr("disabled");
} else {
$("input.group1").attr("disabled", true);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="frmChkForm" id="frmChkForm">
<input type="checkbox" name="chkcc9" id="group1">Check Me <br>
<input type="checkbox" name="chk9[120]" class="group1"><br>
<input type="checkbox" name="chk9[140]" class="group1"><br>
<input type="checkbox" name="chk9[150]" class="group1"><br>
</form>
您可以使用属性选择器来执行此操作,而无需引入ID和类,但是它速度较慢且(imho)较难阅读。
$(this).change
第一个复选框的click事件。
.prop()
代替.attr()
。
这是最新的解决方案。
<form name="frmChkForm" id="frmChkForm">
<input type="checkbox" name="chkcc9" id="group1" />Check Me
<input type="checkbox" name="chk9[120]" class="group1" />
<input type="checkbox" name="chk9[140]" class="group1" />
<input type="checkbox" name="chk9[150]" class="group1" />
</form>
$(function() {
enable_cb();
$("#group1").click(enable_cb);
});
function enable_cb() {
$("input.group1").prop("disabled", !this.checked);
}
下面是使用细节.attr()
和.prop()
。
使用新.prop()
功能:
$("input.group1").prop("disabled", true);
$("input.group1").prop("disabled", false);
该.prop()
功能不可用,因此您需要使用.attr()
。
要禁用复选框(通过设置禁用属性的值),请执行
$("input.group1").attr('disabled','disabled');
并启用(通过完全删除属性)来执行
$("input.group1").removeAttr('disabled');
如果您只使用一个元素,它将永远是最快的DOMElement.disabled = true
。使用.prop()
和.attr()
函数的好处是它们将在所有匹配的元素上运行。
// Assuming an event handler on a checkbox
if (this.disabled)
<form name="frmChkForm" id="frmChkForm">
<input type="checkbox" name="chkcc9" id="chkAll">Check Me
<input type="checkbox" name="chk9[120]" class="chkGroup">
<input type="checkbox" name="chk9[140]" class="chkGroup">
<input type="checkbox" name="chk9[150]" class="chkGroup">
</form>
$("#chkAll").click(function() {
$(".chkGroup").attr("checked", this.checked);
});
具有附加功能,可确保如果选中所有单个复选框,则选中/选中所有复选框:
$(".chkGroup").click(function() {
$("#chkAll")[0].checked = $(".chkGroup:checked").length == $(".chkGroup").length;
});
这是另一个使用JQuery 1.10.2的示例
$(".chkcc9").on('click', function() {
$(this)
.parents('table')
.find('.group1')
.prop('checked', $(this).is(':checked'));
});
$(document).ready(function() {
$('#InventoryMasterError').click(function(event) { //on click
if (this.checked) { // check select status
$('.checkerror').each(function() { //loop through each checkbox
$('#selecctall').attr('disabled', 'disabled');
});
} else {
$('.checkerror').each(function() { //loop through each checkbox
$('#selecctall').removeAttr('disabled', 'disabled');
});
}
});
});
$(document).ready(function() {
$('#selecctall').click(function(event) { //on click
if (this.checked) { // check select status
$('.checkbox1').each(function() { //loop through each checkbox
$('#InventoryMasterError').attr('disabled', 'disabled');
});
} else {
$('.checkbox1').each(function() { //loop through each checkbox
$('#InventoryMasterError').removeAttr('disabled', 'disabled');
});
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="selecctall" name="selecctall" value="All" />
<input type="checkbox" name="data[InventoryMaster][error]" label="" value="error" id="InventoryMasterError" />
<input type="checkbox" name="checkid[]" class="checkbox1" value="1" id="InventoryMasterId" />
<input type="checkbox" name="checkid[]" class="checkbox1" value="2" id="InventoryMasterId" />
$jQuery(function() {
enable_cb();
jQuery("#group1").click(enable_cb);
});
function enable_cb() {
if (this.checked) {
jQuery("input.group1").removeAttr("disabled");
} else {
jQuery("input.group1").attr("disabled", true);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form name="frmChkForm" id="frmChkForm">
<input type="checkbox" name="chkcc9" id="group1">Check Me <br>
<input type="checkbox" name="chk9[120]" class="group1"><br>
<input type="checkbox" name="chk9[140]" class="group1"><br>
<input type="checkbox" name="chk9[150]" class="group1"><br>
</form>