简体   繁体   English

选中 Javascript 中的所有“是”复选框

[英]Check all "yes" checkboxes in Javascript

I am trying to implement a "yes to all" checkbox on my website.我正在尝试在我的网站上实施“对所有人都是”复选框。

This is a sample of part of the code I have so far.这是我目前拥有的部分代码示例。

 $("input:checkbox").each(function () { var pos = $(this).prop("id"); var arr = pos.split("_"); var arr2 = arr[1].split("C"); if (arr2[1] == undefined) { var arr3 = arr[1].split("W"); $(this).addClass(arr3[0]); } else { $(this).addClass(arr2[0]); } }); $(".P2").change(function () { var a = $(".P2:checkbox:checked"); if (a.length = 1) { $(".P3").prop("checked", true); $(".P4").prop("checked", true); $(".P5").prop("checked", true); $(".P6").prop("checked", true); $(".P7").prop("checked", true); $(".P8").prop("checked", true); $(".P9").prop("checked", true); $(".P10").prop("checked", true); $(".P11").prop("checked", true); } });
 <asp:CheckBox ID="P2C1" runat="server" text="&nbsp;&nbsp;Yes to All"/> <asp:CheckBox ID="P3C1" runat="server" text="&nbsp;&nbsp;Yes" /> <asp:CheckBox ID="P3C2" runat="server" text="&nbsp;&nbsp;No" /> <asp:CheckBox ID="P4C1" runat="server" text="&nbsp;&nbsp;Yes" /> <asp:CheckBox ID="P4C2" runat="server" text="&nbsp;&nbsp;No" />

That is just a portion of the checkboxes it goes on in the same pattern.这只是它以相同模式出现的复选框的一部分。 The first box is yes and the second no.第一个方框是,第二个方框不是。 I need to implement the P2 checkbox as a select yes to all and only have the javascript select the checkboxes with an ID of C1 at the end.我需要将 P2 复选框实现为 select 是,并且只有 javascript select 最后带有 ID 为 C1 的复选框。 Currently, when checked the select yes to all checks both yes(C1) and no(C2) boxes.当前,当检查select时,所有检查是(C1)和否(C2)框。 I have tried to change the code to:我试图将代码更改为:

 $(".P2").change(function () {
          var a = $(".P2:checkbox:checked");
          if (a.length = 1) {
              $(".P3C1").prop("checked", true);
              $(".P4C1").prop("checked", true);

But that does not work and it does not check any boxes.但这不起作用,它不会选中任何框。

I know it has to do with the top input function, but I am just not sure what to change.我知道它与顶部输入 function 有关,但我不确定要更改什么。 I don't want to mess with the top input function at all because other things rely on that to work as is.我根本不想弄乱顶部输入 function,因为其他东西依赖于它按原样工作。

I swapped out the JSP tags for native HTML tags.我将 JSP 标签换成原生 HTML 标签。 The example below should adhere to your algorithm.下面的例子应该符合你的算法。

The "Yes to All" should have a special class or ID. “Yes to All”应该有一个特殊的 class 或 ID。

 $('.all').change(function() { $('input[id$="_C1"]:not(.all)').prop('checked', $(this).is(':checked')); $('input[id$="_C2"]:not(.all)').prop('checked', false); }); $('input:not(.all)').change(function() { var id = $(this).attr('id'), parts = id.split(/_/), group = parts[0]; $('input[id^="' + group + '"]:not(#' + id + ')').each(function() { if ($(this).is(':checked')) { $(this).prop('checked', false); } }); });
 body { display: flex; flex-direction: column; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label><input type="checkbox" id="P2_C1" class="all" />Yes to All</label> <label><input type="checkbox" id="P3_C1" />Yes</label> <label><input type="checkbox" id="P3_C2" />No</label> <label><input type="checkbox" id="P4_C1" />Yes</label> <label><input type="checkbox" id="P4_C2" />No</label>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM