从多个MAIN复选框中选择一个复选框,禁用所有其他MAIN框,n启用子复选框

时间:2011-01-05 19:36:06

标签: jquery

<input type="checkbox" id="chkMain" />
<input type="checkbox" id="chkMain1" />
<input type="checkbox" id="chkMain2" />
<input class="child" type="checkbox" id="chk1" disabled="true" />
<input class="child" type="checkbox" id="chk2" disabled="true" />
<input class="child" type="checkbox" id="chk3" disabled="true" />
<input class="child" type="checkbox" id="chk4" disabled="true" />
<input class="child" type="checkbox" id="chk5" disabled="true" />
<input class="child" type="checkbox" id="chk6" disabled="true" />
<input class="child" type="checkbox" id="chk7" disabled="true" />


$(function(){  
        $("input[id^=chkMain]").click ( function() {  
            if( !$(this).is ( ":checked" ) ){       
                $(".child").attr ( "disabled" , true );

            }      
            else{       
                $(".child").removeAttr ( "disabled" );     
            }   
        }); 
    });

当检查chkMain或chkMain1或chkMain2时,这将启用所有子框。 我想要的是:当检查chkMain时,代码应禁用chkMain1和chkMain2,但启用子框。 要么  检查chkMain1时,代码应禁用chkMain和chkMain2,但启用子框。

1 个答案:

答案 0 :(得分:0)

试试这个,诀窍是在jquery中使用not evaluationator选择其他Main复选框:

<input type="checkbox" id="chkMain" />
    <input type="checkbox" id="chkMain1" />
    <input type="checkbox" id="chkMain2" />
    <input class="child" type="checkbox" id="chk1" disabled="true" />
    <input class="child" type="checkbox" id="chk2" disabled="true" />
    <input class="child" type="checkbox" id="chk3" disabled="true" />
    <input class="child" type="checkbox" id="chk4" disabled="true" />
    <input class="child" type="checkbox" id="chk5" disabled="true" />
    <input class="child" type="checkbox" id="chk6" disabled="true" />
    <input class="child" type="checkbox" id="chk7" disabled="true" />
    <script>
        $
        (
            function()
            {           
                $("input[id^=chkMain]").click 
                ( 
                    function() 
                    {               
                        var otherCks = $("input[id^=chkMain]").not(this);
                        if( !$(this).is( ":checked" ))
                        {                        
                            $(".child").attr("disabled" , true );              
                            otherCks.removeAttr ( "disabled" );                  
                        }                   
                        else
                        {                        
                            $(".child").removeAttr ( "disabled" );                  

                            otherCks.attr("disabled" , true)
                        }            
                    }
                );      
            }
        );
    </script>