簡體   English   中英

復選框子父 JavaScript

[英]Checkbox child parent JavaScript

我有一個問題,我需要創建一個全選復選框,該復選框已經 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>

它看起來像這樣?

$(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());
    })
});

})

我設法解決了! 下面是 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);
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM