Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

c# - Checkbox child parent JavaScript

I have a question, I need to create a checkbox that selects all, which is already running 100% but I can't create a "parent" checkbox that only marks his child checkboxes

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>
question from:https://stackoverflow.com/questions/65845313/checkbox-child-parent-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It would look something like this?

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

})


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...