<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 7 年前。
<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 7 年前。
我正在尝试实现 JavaScript 来更新相应 div 中的复选框兄弟。我有多个具有相同复选框选项的 div:#check_all_checkboxes、#check_none 和 .others。我希望 div 更新它们的 sibling ,但 JavaScript 不会更新 sibling 。我可以对兄弟类或 ID 进行硬编码,JavaScript 将在第一个 div 上成功执行,但不会在后续的 div 上执行。
// Select ALL checkboxes
$('#check_all_checkboxes').click(function () {
if ( $(this).is(':checked') ){
$(this).siblings('.others').prop('checked', true);
$(this).siblings('#check_none').removeAttr('checked');
}
});
// Select NO checkbox(es)
$('#check_none').click(function () {
if ( $(this).is(':checked') ){
$(this).siblings('.others').removeAttr('checked');
$(this).siblings('#check_all_checkboxes').removeAttr('checked');
}
});
// Select other checkbox(es)
$('.others').click(function () {
$(this).siblings('#check_none').removeAttr('checked');
$(this).siblings('#check_all_checkboxes').removeAttr('checked');
});
最佳答案
这篇文章建议您使用 .prop('checked', false) 可能会更好: .prop('checked',false) or .removeAttr('checked')?
关于javascript - 如何使用 JavaScript 更新兄弟复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31930897/