我正在尝试选中一个复选框,并让该操作检查我表单中的其余复选框。我如何使用类来完成这个任务?
<script>
$(".global").on("click", function() {
var all = $(this);
$('.checkbtn').each(function() {
$(this).prop("checked", all.prop("checked"));
});
});
</script>
<div>
<input type="checkbox" class ="global" id="select-all" name="selectAll" value=""/> All
<input type="checkbox" class ="checkbtn" name="practice" value=""/> 1
<input type="checkbox" class ="checkbtn" name="filename" value=""/> 2
<input type="checkbox" class ="checkbtn" name="comment" value=""/> 3
</div>发布于 2015-12-28 17:54:08
你不必要地使这项任务复杂化了。往这边走:
$(function() {
$(".global").on("change", function() {
var state = this.checked;
$('.checkbtn').prop("checked", state);
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="global" id="select-all" name="selectAll" value="" />All
<input type="checkbox" class="checkbtn" name="practice" value="" />1
<input type="checkbox" class="checkbtn" name="filename" value="" />2
<input type="checkbox" class="checkbtn" name="comment" value="" />3
https://stackoverflow.com/questions/34498278
复制相似问题