未选中所需的复选框

时间:2014-02-12 19:24:33

标签: javascript jquery coldfusion

我想要的是什么:

当我点击“报告”复选框时,它应该检查它下面的所有复选框。我的意思是 “报告”,“电子邮件组”,“SMS组”,“国家组”,“呼叫结果”下方的所有复选框 分组“如图所示。我认为这是理想情况的样子

目前正在发生的事情:

当我点击“报告”时,只会选中要检查的复选框 “报告广告系列”以“报告新测试”。什么都没发生 “电子邮件组”及其子项复选框,类似于SMS Group,States Group 和呼叫结果组。因此,我必须手动点击“电子邮件组”和其他组来选择其子女。

我在jsfiddle这里定义了以下函数:

function parentCheckboxCheck(obj, childrenClass, parentId ){
    if(parentId == '' ){
        if($(obj).is(':checked')){
            $(childrenClass).attr('disabled',false);
            $(childrenClass).attr('checked',true);
        }
        else {
            $(childrenClass).attr('checked',false);
            $(childrenClass).attr('disabled',true);
        }

    }else{
        if($(obj).is(':checked') && $('#'+parentId).is(':checked')){
            $(childrenClass).attr('disabled',false);
            $(childrenClass).attr('checked',true);
        }
        else {
            $(childrenClass).attr('checked',false);
            $(childrenClass).attr('disabled',true);
        }
    }
}

JS小提琴链接:http://jsfiddle.net/VKV7t/1/

请告诉我这里的错误或需要进行哪些修改?

这是我的冷冻代码:

<ul class="list_permisstion">

            <cfif listfindNoCase(listAllPermission,Reporting_Title) GT 0>
                <span class="label_permission">
                    <cfset Checked_checkbox = false>
                    <cfif listfindNoCase(checkedAllPermistion,Reporting_Title) GT 0>
                        <cfset Checked_checkbox = true>
                    </cfif> 
                    <cfinput type="checkbox" name="#Reporting_Title#" id="Reporting" onclick = "parentCheckboxCheck(this,'.ReportingChilds', '')" value="#Reporting_Title#" checked="#Checked_checkbox#"> 
                    <label class="label_permission" for="Reporting">#Reporting_Title#</label>
                </span>
            </cfif>

            <ul class="list_permisstion">

                <cfloop array="#Reporting_List#" index="ListLoop" >

                    <cfif listfindNoCase(listAllPermission,ListLoop) GT 0>
                        <li>
                            <cfset Checked_checkbox = false>
                            <cfif listfindNoCase(checkedAllPermistion,ListLoop) GT 0>
                                <cfset Checked_checkbox = true>
                            </cfif>
                            <cfinput type="checkbox" name="#ListLoop#" id="ListLoop" class="ReportingChilds" value="#ListLoop#" checked="#Checked_checkbox#"> 
                            <label for="label_permission">#ListLoop#</label>
                        </li>
                    </cfif>

                </cfloop>


            </ul>
            <!--- For Email Group --->
            <ul class="list_permisstion">

                <cfif listfindNoCase(listAllPermission,Reporting_Group_Title) GT 0>
                    <span class="label_permission"> 
                        <cfset Checked_checkbox = false>
                        <cfif listfindNoCase(checkedAllPermistion,Reporting_Group_Title) GT 0>
                            <cfset Checked_checkbox = true>
                        </cfif> 
                        <cfinput type="checkbox" name="#Reporting_Group_Title#" id="ReportGroup" onclick = "parentCheckboxCheck(this,'.reportGroupChilds','Reporting')" class="reportingChilds" value="#Reporting_Group_Title#" checked="#Checked_checkbox#"> 
                        <label for="ReportGroup">#Reporting_Group_Title#</label> 
                    </span>
                </cfif>

                <cfloop array="#Reporting_Group_List#" index="ListLoop" >

                    <cfif listfindNoCase(listAllPermission,ListLoop) GT 0>
                        <li>
                            <cfset Checked_checkbox = false>
                            <cfif listfindNoCase(checkedAllPermistion,ListLoop) GT 0>
                                <cfset Checked_checkbox = true>
                            </cfif>
                            <cfinput type="checkbox" name="#ListLoop#" id="ListLoop" class="reportGroupChilds" value="#ListLoop#" checked="#Checked_checkbox#"> 
                            <label for="ReportGroup">#ListLoop#</label>
                        </li>
                    </cfif>

                </cfloop>

            </ul>
             <!--- For SMS Group --->
             <ul class="list_permisstion">

                <cfif listfindNoCase(listAllPermission,Reporting_SMS_Group_Title) GT 0>
                    <span class="label_permission"> 
                        <cfset Checked_checkbox = false>
                        <cfif listfindNoCase(checkedAllPermistion,Reporting_SMS_Group_Title) GT 0>
                            <cfset Checked_checkbox = true>
                        </cfif> 
                        <cfinput type="checkbox" name="#Reporting_SMS_Group_Title#" id="ReportSMSGroup" onclick = "parentCheckboxCheck(this,'.reportSMSGroupChilds','Reporting')" class="reportingSMSChilds" value="#Reporting_SMS_Group_Title#" checked="#Checked_checkbox#"> 
                        <label for="ReportSMSGroup">#Reporting_SMS_Group_Title#</label> 
                    </span>
                </cfif>

                <cfloop array="#Reporting_SMSGroup_List#" index="ListLoop" >

                    <cfif listfindNoCase(listAllPermission,ListLoop) GT 0>
                        <li>
                            <cfset Checked_checkbox = false>
                            <cfif listfindNoCase(checkedAllPermistion,ListLoop) GT 0>
                                <cfset Checked_checkbox = true>
                            </cfif>
                            <cfinput type="checkbox" name="#ListLoop#" id="ListLoop" class="reportSMSGroupChilds" value="#ListLoop#" checked="#Checked_checkbox#"> 
                            <label for="ReportSMSGroup">#ListLoop#</label>
                        </li>
                    </cfif>

                </cfloop>

            </ul> 
            <!--- For STATES GROUP --->
            <ul class="list_permisstion">

                <cfif listfindNoCase(listAllPermission,Reporting_States_Group_Title) GT 0>
                    <span class="label_permission"> 
                        <cfset Checked_checkbox = false>
                        <cfif listfindNoCase(checkedAllPermistion,Reporting_States_Group_Title) GT 0>
                            <cfset Checked_checkbox = true>
                        </cfif> 
                        <cfinput type="checkbox" name="#Reporting_States_Group_Title#" id="ReportSTATESGroup" onclick = "parentCheckboxCheck(this,'.reportSTATESGroupChilds','Reporting')" class="reportingSTATESChilds" value="#Reporting_States_Group_Title#" checked="#Checked_checkbox#"> 
                        <label for="ReportSTATESGroup">#Reporting_States_Group_Title#</label> 
                    </span>
                </cfif>

                <cfloop array="#Reporting_StatesGroup_List#" index="ListLoop" >

                    <cfif listfindNoCase(listAllPermission,ListLoop) GT 0>
                        <li>
                            <cfset Checked_checkbox = false>
                            <cfif listfindNoCase(checkedAllPermistion,ListLoop) GT 0>
                                <cfset Checked_checkbox = true>
                            </cfif>
                            <cfinput type="checkbox" name="#ListLoop#" id="ListLoop" class="reportSTATESGroupChilds" value="#ListLoop#" checked="#Checked_checkbox#"> 
                            <label for="ReportSTATESGroup">#ListLoop#</label>
                        </li>
                    </cfif>

                </cfloop>

            </ul>

            <!--- For CALL RESULTS Group --->

            <ul class="list_permisstion">

                <cfif listfindNoCase(listAllPermission,Reporting_CallResults_Group_Title) GT 0>
                    <span class="label_permission"> 
                        <cfset Checked_checkbox = false>
                        <cfif listfindNoCase(checkedAllPermistion,Reporting_CallResults_Group_Title) GT 0>
                            <cfset Checked_checkbox = true>
                        </cfif> 
                        <cfinput type="checkbox" name="#Reporting_CallResults_Group_Title#" id="ReportSTATESGroup" onclick = "parentCheckboxCheck(this,'.reportCALLRESULTSGroupChilds','Reporting')" class="reportingCALLRESULTSChilds" value="#Reporting_CallResults_Group_Title#" checked="#Checked_checkbox#"> 
                        <label for="ReportCALLRESULTSGroup">#Reporting_CallResults_Group_Title#</label> 
                    </span>
                </cfif>

                <cfloop array="#Reporting_CallResultsGroup_List#" index="ListLoop" >

                    <cfif listfindNoCase(listAllPermission,ListLoop) GT 0>
                        <li>
                            <cfset Checked_checkbox = false>
                            <cfif listfindNoCase(checkedAllPermistion,ListLoop) GT 0>
                                <cfset Checked_checkbox = true>
                            </cfif>
                            <cfinput type="checkbox" name="#ListLoop#" id="ListLoop" class="reportCALLRESULTSGroupChilds" value="#ListLoop#" checked="#Checked_checkbox#"> 
                            <label for="ReportCALLRESULTSGroup">#ListLoop#</label>
                        </li>
                    </cfif>

                </cfloop>

            </ul>

</ul> 

2 个答案:

答案 0 :(得分:0)

您拥有<ul class="list_permisstion">区块中的所有复选框。如果我理解正确,您需要检查该块中的所有复选框。所以基本上你需要获得该块中的所有复选框。 jQuery find()函数搜索所选元素的后代,在您的情况下将是<ul class="list_permisstion">。检查http://api.jquery.com/find/

示例函数看起来像那样,其中childrenClass是您需要检查的复选框容器的类名。

function parentCheckboxCheck(obj, childrenClass, parentId ){
        if($(obj).is(':checked')){
            $(childrenClass).find('input[type="checkbox"]').attr('disabled',false);
            $(childrenClass).find('input[type="checkbox"]').attr('checked',true);
        }
        else {
            $(childrenClass).find('input[type="checkbox"]').attr('checked',false);
            $(childrenClass).find('input[type="checkbox"]').attr('disabled',true);
        }
}

答案 1 :(得分:0)

从小提琴的角度来看,我认为你有几个选择。在您对parentCheckboxCheck的调用中,您可以将每个应该受影响的类添加到childrenClass参数中,例如在检查报告时,调用将是parentCheckboxCheck(这是'.reportGroupChilds .reportSMSGroupChilds .reportSMSGroupChilds等...)并忽略parentID。其他选项是更改parentCheckboxcheck以使用JQuery遍历DOM以获取该复选框之后的所有元素并在那里检查/禁用它们。

好吧,第一个选项的代码将在你的html中

<cfinput type="checkbox" name="#Reporting_Title#" id="Reporting" onclick="parentCheckboxCheck(this,'.ReportingChilds, .reportGroupChilds, .reportSMSGro‌​upChilds, .reportSTATESGroupChilds, .reportCALLRESULTSGroupChilds', '')" value="#Reporting_Title#" checked="#Checked_checkbox#"> 

对于第二种选择,我认为这样的事情会起作用。

   function parentCheckboxCheck(obj, childrenClass, parentId ){
        var el = $(obj);
        el.closest('.list_permisstion')
            .find(':checkbox')
            .not(this).each(function(){
                $(this).prop('checked', el.is(':checked'));
            });
    };

小提琴:http://jsfiddle.net/LYSRp/1/

我不清楚您想要禁用或启用的功能,但我认为这具有您正在寻找的检查功能。