Treeview 父子节点选中的级联操作

      选中父节点,同时选中子节点。 选中子节点,自动勾选上父节点,当所有的子节点都没有选中的时候,又自动取消父节点的勾选。

 

ExpandedBlockStart.gif 代码
    < script src = " http://www.cnblogs.com/js/jquery-1.4.2.js "  type = " text/javascript " >< / script>
     < script language = " javascript "  type = " text/javascript " >
        $(document).ready(Bind);
        
function  Bind() {
            BindTreeView(
" tvMenu " true true );
        }
        
function  BindTreeView(treeViewId,selectChild,selectParent) {

            $(
" # "   +  treeViewId  +   "  input " ).click( function () {
                
if  (selectChild) {
                    
var  divItem  =  $( this ).parent().parent().parent().parent().next();
                    
if  (divItem[ 0 !=   null ){  //  屏蔽选最后一个时,会弹出错误的漏洞
                         if  (divItem[ 0 ].tagName  ==   " DIV " )
                            divItem.find(
" input " ).attr( " checked " , $( this ).get( 0 ).checked); 
                    }
                }
                
if  (selectParent) {
                    SelectParentNode(treeViewId, $(
this ));
                }
            });
        }

        
function  SelectParentNode(treeViewId,node) {
            
if  (node.get( 0 !=   null  ){
                
if  (node.get( 0 ).checked  ==   true ) {
                    
var  pdiv  =  node.parent().parent().parent().parent().parent();
                    
if  (pdiv.get( 0 ).id  !=  treeViewId) {
                        
                        pdiv.prev().find(
" input " ).attr( " checked " , node.get( 0 ).checked);

                        SelectParentNode(treeViewId, pdiv.prev().find(
" input " ));
                    }
                } 
else  {  //  取消所有子的选择,父节点也取消
                     var  pdiv  =  node.parent().parent().parent().parent().parent();
                    
if  (pdiv.get( 0 ).id  !=  treeViewId) {

                        
var  divItem  =  node.parent().parent().parent().parent().parent();
                        
if  (divItem[ 0 !=   null ) {
                            
if  (divItem[ 0 ].tagName  ==   " DIV " ) {
                                
if  (divItem.find( " input[type='checkbox']:checked " ).size()  ==   0  ) {
                                    pdiv.prev().find(
" input " ).attr( " checked " false );
                                }
                            }
                        }

                        SelectParentNode(treeViewId, pdiv.prev().find(
" input " ));
                    }
                }
            }
        }
    
< / script>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值