【问题标题】:Checkbox child parent JavaScript复选框子父 JavaScript
【发布时间】:2021-01-22 12:35:27
【问题描述】:

我有一个问题,我需要创建一个全选复选框,该复选框已经 100% 运行,但我无法创建仅标记其子复选框的“父”复选框

enter code here
<input type="checkbox" title="todos" id="checkTodos"  name="checkTodos"> <label><b>Marcar/Desmarcar Todos</b></label>
<div class="Registro">
        <input type="checkbox" checked="checked" class="chkTela" />        
        <asp:HyperLink runat="server" ID="hlTela">
        <span class="Tela">Tela <%#Eval("Descricao") %></span></asp:HyperLink>    
        <ul>
            <asp:Repeater ID="rptTipoCritica" runat="server" OnItemCommand="rptTipoCriticas_ItemCommand">
            <ItemTemplate>
                <li>                
                <input name="chkCritica" id='chk<%#Eval("Codigo") %>'  value='<%#Eval("Codigo") %>'  type="checkbox"  <%# EmAnalise && (Eval("Executada").ToString() == "False") ? "checked='checked' ": "disabled=disabled" %> />
                <asp:Image ID="imgExecutada" runat="server" />
                <asp:Button ID="btnVerificar" runat="server" Text="Verificar" CommandArgument='<%#Eval("Codigo") %>' CommandName="Verificar" />            
                <label for='chk<%#Eval("Codigo") %>'><%#Eval("Descricao") %></label></li>
            </ItemTemplate>
            </asp:Repeater>
        </ul>
    </div>
<script type="text/javascript">
        $(function() {
            $(".chkTela").on("click",
            function() {
                propagarSelecao($(this));
            });
            
        });
        $("#checkTodos").change(function () {
            $("input:checkbox").prop('checked', $(this).prop("checked"));
        });
        function propagarSelecao($chk) {
            $("input[type=checkbox]:not(:disabled)", $chk.parent()).attr("checked", $chk.attr("checked"));
        }
        
    </script>

【问题讨论】:

  • 我是否正确理解您的问题:如果选择了父母,您想选择所有孩子吗?!
  • @AbbasAkhundov 完全正确,我的复选框 100% 正常工作,但我需要复选框“爸爸”

标签: javascript c#


【解决方案1】:

看起来像这样?

$(function () {
$('input:checkbox.main-checkbox').click(function () {
    var array = [];
    var parent = $(this).closest('.main-parent');
    //check or uncheck sub-checkbox
    $(parent).find('.sub-checkbox').prop("checked", $(this).prop("checked"))
    //push checked sub-checkbox value to array
    $(parent).find('.sub-checkbox:checked').each(function () {
        array.push($(this).val());
    })
});

})

【讨论】:

    【解决方案2】:

    我设法解决了! 下面是jQuery代码

    $('input:checkbox.parent').click(function () {
            if (this.checked) {
                $(this).parents('li').children('input[type=checkbox]').prop('checked', true);
            }
            $(this).parent().find('input[type=checkbox]').prop('checked', this.checked);
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-27
      • 2014-05-01
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多