gpt4 book ai didi

javascript - 如何通过单击复选框或其文本来选中所有复选框

转载 作者:行者123 更新时间:2023-11-28 19:58:12 25 4
gpt4 key购买 nike

我有一个复选框,它将调用下面给出的 jquery 函数来检查页面中的所有复选框:

//to check all checkbox
$(document).ready(function () {
$("#checkAll").click(function () {
$("input:checkbox").prop('checked', $(this).prop('checked'));
});
});

但是当我单击下面的“全选”文本时,我也想调用相同的 jquery 函数,但它不起作用:

<input type="checkbox" id="checkAll" /> 
<a id="checkAll" onclick="document.getElementById('checkAll').checked=true" style="cursor:pointer"> Select All </a>

最佳答案

更新:

ID 不能在文档中分配两次! ID 是选择元素的唯一 Key,如果要选择两个元素,则必须使用 class立刻!

<input type="checkbox" class="checkAll" /> 
<a class="checkAll" onclick="document.getElementsByClassName('checkAll')[0].checked=true" style="cursor:pointer"> Select All </a>

或者解决问题的另一种方法是使用Label标签并将您的input:checkbox包装到其中,就像

<label>
<input type="checkbox" class="checkAll" />
Select All
</label>

关于javascript - 如何通过单击复选框或其文本来选中所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22129544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com