简体   繁体   English

如何取消选中jQuery中最接近的标题复选框-具有相同类的多行中的多标题

[英]How to uncheck closest header checkbox in jquery - multiheader in multirows with same class

How to uncheck closest header checkbox in jquery. 如何取消选中jQuery中最接近的标题复选框。 There are multiple headers in different rows, checkbox in header needs to be unchecked when all the child checkboxes are unticked. 在不同的行中有多个标题,当所有子复选框都未选中时,标题中的复选框需要取消选中。

Header checkbox is in class 'jq_idtHeaderEdit' and child checkboxes are in class 'jq_border_label'. 标题复选框在类'jq_idtHeaderEdit'中,子复选框在类'jq_border_label'中。

1- On child checkbox change event, how to check if all the child checkboxes in class 'jq_border_label' are unticked? 1-关于子复选框更改事件,如何检查是否选中了“ jq_border_label”类中的所有子复选框?

2- If all the child checkboxes are unticked then untick the header in closest class 'jq_idtHeaderEdit'. 2-如果未选中所有子复选框,则取消选中最接近的类'jq_idtHeaderEdit'中的标题。

Thats how the table looks like; 那就是桌子的样子;

        <tbody>

        <tr class="jq_idtHeaderRow"></tr>
        <tr class="jq_idtListRow"></tr>
        <tr class="jq_idtHeaderRow"></tr>
        <tr class="jq_idtListRow">
            <td class="jq_idtContentEdit" valign="top" colspan="6">
                <table>
                    <tbody>
                        <tr>
                            <td class="jq_idtContentEdit"></td>
                            <td class="jq_idtContentEdit"></td>
                            <td class="jq_idtContentEdit"></td>
                        </tr>

Expanded table 展开表

            <table id="ctl00_ContentPlaceHolder1_tabContainer_tabDetails_idtDiscipline_tblEdit">

            <tbody>
                <tr class="jq_idtHeaderRow"></tr>
                <tr class="jq_idtListRow"></tr>
                <tr class="jq_idtHeaderRow"></tr>
                <td class="jq_idtHeaderEdit" colspan="6">

                    <input id="ctl00_ContentPlaceHolder1_tabContainer_tabDetails_idtDiscipline_ctl24" type="checkbox" value="160" checked="checked" name="ctl00$ContentPlaceHolder1$tabContainer$tabDetails$idtDiscipline$ctl24"></input>
                    <label for="ctl00_ContentPlaceHolder1_tabContainer_tabDetails_idtDiscipline_ctl24"></label>

                </td>
                <tr class="jq_idtListRow">
                    <td class="jq_idtContentEdit" valign="top" colspan="6">
                        <table>
                            <tbody>
                                <tr>
                                    <td class="jq_idtContentEdit">
                                        <div class="jq_border_label_container">
                                            <div class="border_label_box">
                                                <span class="jq_border_label" style="background-color: whitesmoke; color: rgb(0, 0, 0);"
                                                    <input id="ctl00_ContentPlaceHolder1_tabContainer_tabDetails_idtDiscipline_ctl32" name="ctl00$ContentPlaceHolder1$tabContainer$tabDetails$idtDiscipline$ctl32" checked="checked" value="197" type="checkbox">

在此处输入图片说明

Fiddle 小提琴

https://jsfiddle.net/m1Lmg616/6/ https://jsfiddle.net/m1Lmg616/6/

I want Header-2 to be unticked when all his child checkboxes are unticked. 我希望未选中他的所有子复选框时都选中Header-2。

I have tried this in 'onchange' event of child checkbox but no luck so far; 我已经在子复选框的“ onchange”事件中尝试过此方法,但到目前为止还没有运气。

var jqo = $(this);
if ($(jqo).closest('.jq_border_label').find('input[type=checkbox]').prop('checked').length > 0) {
        alert('at least one ticked');
    }
$('.chkChildHeader').on('change', function () {
        var checkedNum = $('.jq_border_label > input[type="checkbox"]:checked').length;
        if (checkedNum > 0) {
            $(this).closest('.jq_idtHeaderEdit').find('.chkBoxHeader').prop('checked', true);
        } else {
            $(this).closest('.jq_idtHeaderEdit').find('.chkBoxHeader').prop('checked', false);
        }
    });

I have prepared the following sample demo and below is the js fiddle link: https://jsfiddle.net/shrawanlakhe/41v9s4vv/15/ 我已经准备了以下示例演示,下面是js小提琴链接: https : //jsfiddle.net/shrawanlakhe/41v9s4vv/15/

I think you can use this as reference to solve your problem since you didn't provide fiddle to your problem.. 我认为您可以以此为参考来解决您的问题,因为您没有为问题提供任何帮助。

Fiddle shows that even if only one checkbox is checked,header checkbox is checkbox and if none of the child checkboxes are checked ,header checkbox is unticked. Fiddle显示,即使仅选中一个复选框,标题复选框也是复选框,如果未选中所有子复选框,则标题复选框都不会被选中。

If you want to checkbox only if all the child checkboxes are checked, then below is the code you can use 如果仅在所有子复选框都选中的情况下才想复选框,那么下面是您可以使用的代码

$('.chkChildHeader').on('change', function () {
        var length = $('.jq_border_label > input[type="checkbox"]').length;
        var checkedNum = $('.jq_border_label > input[type="checkbox"]:checked').length;

        if (length === checkedNum) {
            $(this).closest('.jq_idtHeaderEdit').find('.chkBoxHeader').prop('checked', true);
        }
        if (!checkedNum) {
            $(this).closest('.jq_idtHeaderEdit').find('.chkBoxHeader').prop('checked', false);
        }
    });

and below is the fiddle link: https://jsfiddle.net/shrawanlakhe/1ecqs3bu/1/ 下面是小提琴链接: https : //jsfiddle.net/shrawanlakhe/1ecqs3bu/1/

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

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