简体   繁体   English

在检查孩子复选框父母没有检查老虎钳反之亦然

[英]on checking the child checkbox parent does not checked vise versa Works perfectly

Hi I've these check boxes in asp.net repeater when I click the parent check box as you can see in this picture all child check boxes get check, in the other hand when I check the child check box the parent did not get selected. 嗨,当我单击父复选框时,我在asp.net repeater这些复选框,如您在这张图片中看到的,所有子复选框都被选中,另一方面,当我选中子复选框时,父复选框未被选中。 I'm using Jquery to achieve this My current ASP.NET code is as given below 我正在使用Jquery实现此目的。我当前的ASP.NET代码如下所示

<ul id='<%# "p_"+Eval("ApplicationId") %>' class="child-ul-hide child-ul">
                                                    <asp:Repeater ID="rptrPages" runat="server" OnItemDataBound="rptrPages_ItemDataBound">
                                                        <ItemTemplate>
                                                            <li>
                                                         <%-- <span id='<%# "sp_"+Eval("PageId") %>' class="parent arrow-right" state="hide"></span>--%>
                                                                <asp:Label ID="lblSpaces" runat="server" Text='<%# Eval("Spaces") %>'></asp:Label>
                                                                <asp:Literal ID="ltrArrowSpan" runat="server"></asp:Literal>
                                                                <label>
                                                                    <asp:CheckBox ID="chkPage" runat="server" Text='<%# Eval("PageTitle") %>'
                                                                        CssClass='<%# "page Pid_"+ Eval("ParentId")%>' PageId='<%# Eval("PageId") %>' ParentId='<%# Eval("ParentId") %>'
                                                                        Checked='<%# Eval("IsEnable")== null ? false: Convert.ToBoolean(Eval("IsEnable").ToString())  %>' />
                                                                </label>
                                                                <asp:HiddenField ID="hdPageId" runat="server" Value='<%# Eval("PageId") %>' />

and my JavaScript code is as following as you can see I'd tried a couple of other stuff and used Intermediate snippiest too but when they are invited to ASP.NET Nothing goes as planed. 我的JavaScript代码如下所示,您可以看到我尝试了其他一些东西,也使用了中间代码段,但是当它们被邀请使用ASP.NET一切都没有按计划进行。 many of you might suggest to use <input type='checkbox'/> sorry I can't do that 你们中许多人可能建议使用<input type='checkbox'/>抱歉,我不能这样做

$(document).ready(function () {

        /* Hide Show Tree lis
         *******************************************/
        $(".parent").click(function () {

            var ID = $(this).attr("id");
            var state = $("#" + ID).attr("state");
            if (state == "hide") {

                $("#" + ID).attr('state', 'show');
                $("#" + ID).addClass("arrow-down").removeClass("arrow-right");
                ID = ID.replace("s", "");
                $("#" + ID).addClass("child-ul-show").removeClass("child-ul-hide");
            }
            else {

                $("#" + ID).attr('state', 'hide');
                $("#" + ID).addClass("arrow-right").removeClass("arrow-down");
                ID = ID.replace("s", "");
                $("#" + ID).addClass("child-ul-hide").removeClass("child-ul-show");
            }
        });

    //    /* START Parent Enable CheckBox Click Event
    //   *********************************************/
        $(".page input[type='checkbox']").click(function() {

            var PageId = $(this).parent('.page').attr("PageId");

            var parentId = $(this).parent('.page').attr("ParentId");

            var EnableStatus = $(this).prop("checked");

            // Check/ Uncheck Controls Checkboxes
            $("." + PageId + " input[type='checkbox']").prop('checked', EnableStatus);

            //Check unCheck Childs Checkboxes
            $(".Pid_" + PageId + " input[type='checkbox']").prop('checked', EnableStatus);


            $(".Pid_" + PageId + " input[type='checkbox']").each(function() {
                PageId = $(this).parent('.page').attr("PageId");
                EnableStatus = $(this).prop("checked");
                $(".Pid_" + PageId + " input[type='checkbox']").prop('checked', EnableStatus);
            });

            //$(".page input[type='checkbox']").prop('checked', false);
            //$(".Pid_" + ParentId + " input[type='checkbox']").each(function() {
            //    debugger;
            //    EnableStatus = $(this).prop("checked");
            //    if (EnableStatus) {
            //        $(".page input[type='checkbox']").prop('checked', EnableStatus);
            //        return false;
            //    }
            //});
            debugger;
            parentId = parseInt(parentId);
            if (parentId>0) {
                $("input[type='checkbox'][ParentId^='" + parentId + "']").prop('checked', true);

            }


            /*/ */
            //$('input[name=' + parentId + '][value=Done][type=checkbox]:not(:checked)').attr({
            //    checked: 'checked',
            //    disabled: 'disabled'
            //});



        });

What I can see is that you've a single check box that at a time is acting as a parent as well as a child, I don't have any idea what made you to do that. 我看到的是,您有一个复选框同时充当父母和孩子,我不知道是什么使您这么做。 Lets get to the answer. 让我们得到答案。
Add an attribute to the class as ParentId='<%# Eval("ParentId") get that attribute 将属性添加到类中,因为ParentId='<%# Eval("ParentId")获取该属性

                var ParentId = $(this).parent('.page').attr("ParentId");

use the JavaScript parseInt to parse your id as integer than do this one 使用JavaScript parseInt将您的ID解析为整数,而不是此ID

   ParentId= parseInt(ParentId);
            if (ParentId > 0) {

                $(".p_" + ParentId + " input[type='checkbox']").prop('checked', false);
                $(".Pid_" + ParentId + " input[type='checkbox']").each(function () {

                    EnableStatus = $(this).prop("checked");
                    if (EnableStatus)
                    {
                        $(".p_" + ParentId + " input[type='checkbox']").prop('checked', EnableStatus);
                    }

                });
            }

Hope this will help you 希望这个能对您有所帮助

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

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