隨筆-348  評論-598  文章-0  trackbacks-0

          下面的代碼是,當選中父節點,則子節點全選;選中任意一個子節點,則選中父節點

          jQuery.noConflict();//與JSF搭配需要這句話,不然會有沖突
                  jQuery(document).ready(function() {
                  
          // Add click event listener to each checkbox in the tree page
                  // Note! Using this simple selector assumes that there are no other 
                  // checkboxes on the page, if there are other checkboxes then
                  // selector should be changed    
                      jQuery(":checkbox").click(function(){
                          updateChildren(
          this);
                          updateParent(
          this);
                       }
          );
                  }
          );
                  
          </script>
                  
          <script type="text/javascript">
                  
          function updateChildren(currentCheckBox)
                  
          {
                      
          // Get state of current checkbox (true or false)
                      var state = currentCheckBox.checked;
                      
                      
          // Get parent TABLE, where current checkbox is places
                      var parentTables = jQuery(currentCheckBox).parents("table"); 
                      
          var parentTable = parentTables[0];    
                  
                      
          // Get DIV where child nodes with checkboxes are situated
                      // See http://docs.jquery.com/Traversing/ to get better uderstanding of
                      // parents() and next()        
                      var childDivs = jQuery(parentTable).next("div");    
                      
          if(    childDivs.length > 0 )
                      
          {
                          
          var childDiv = childDivs[0];        
                      
                          
          // Iterate over all child nodes checkboxes and set same state as the 
                          // current checkbox state
                          jQuery(childDiv).contents().find(":checkbox").each(function() {
                              
          this.checked = state;
                          }
          );
                      }


                  }

                  
                  
          //更新父節點的方法,如果子節點全部選中則父節點選中,如果子節點中有一個選中,則父節點也選中
                  function updateParent(currentCheckbox) {
                      
          var parentDivs = jQuery(currentCheckbox).parents("div");
                      
          var parentDiv = parentDivs[0];    

                      
          var hasSelected = false;
                      
                      jQuery(parentDiv).contents().find(
          ":checkbox").each(function() {
                          
          if(this.checked) {
                              hasSelected 
          = true;
                          }

                      }
          );
                      
                      
          var parentTables = jQuery(parentDiv).prev("table");
                      
          if(parentTables.length > 0)
                      
          {
                          
          var parentTable = parentTables[0];
                      
                          
          var parentCheckboxes = jQuery(parentTable).find(":checkbox");
                          
          var parentCheckbox = parentCheckboxes[0];
                          
                          parentCheckbox.checked 
          = hasSelected ;
                      }


                  }

          下面的代碼是,當選中父節點,則子節點全選;選中所有子節點,則選中父節點
          jQuery.noConflict();//與JSF搭配需要這句話,不然會有沖突
                  jQuery(document).ready(function() {
                  
          // Add click event listener to each checkbox in the tree page
                  // Note! Using this simple selector assumes that there are no other 
                  // checkboxes on the page, if there are other checkboxes then
                  // selector should be changed    
                      jQuery(":checkbox").click(function(){
                          updateChildren(
          this);
                          updateParent(
          this);
                       }
          );
                  }
          );
                  
          </script>
                  
          <script type="text/javascript">
                  
          function updateChildren(currentCheckBox)
                  
          {
                      
          // Get state of current checkbox (true or false)
                      var state = currentCheckBox.checked;
                      
                      
          // Get parent TABLE, where current checkbox is places
                      var parentTables = jQuery(currentCheckBox).parents("table"); 
                      
          var parentTable = parentTables[0];    
                  
                      
          // Get DIV where child nodes with checkboxes are situated
                      // See http://docs.jquery.com/Traversing/ to get better uderstanding of
                      // parents() and next()        
                      var childDivs = jQuery(parentTable).next("div");    
                      
          if(    childDivs.length > 0 )
                      
          {
                          
          var childDiv = childDivs[0];        
                      
                          
          // Iterate over all child nodes checkboxes and set same state as the 
                          // current checkbox state
                          jQuery(childDiv).contents().find(":checkbox").each(function() {
                              
          this.checked = state;
                          }
          );
                      }


                  }

                  
                  
          //更新父節點的方法,如果子節點全部選中則父節點選中,如果子節點中有一個未選中,則父節點也未選中
                  function updateParent(currentCheckbox) {
                      
          var parentDivs = jQuery(currentCheckbox).parents("div");
                      
          var parentDiv = parentDivs[0];    

                      
          var hasSelected = true;
                      
                      jQuery(parentDiv).contents().find(
          ":checkbox").each(function() {
                          
          if(!this.checked) {
                              hasSelected 
          = false;
                          }

                      }
          );
                      
                      
          var parentTables = jQuery(parentDiv).prev("table");
                      
          if(parentTables.length > 0)
                      
          {
                          
          var parentTable = parentTables[0];
                      
                          
          var parentCheckboxes = jQuery(parentTable).find(":checkbox");
                          
          var parentCheckbox = parentCheckboxes[0];
                          
                          parentCheckbox.checked 
          = hasSelected ;
                      }


                  }


          ---------------------------------------------------------
          專注移動開發

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2009-12-27 22:52 TiGERTiAN 閱讀(2768) 評論(0)  編輯  收藏 所屬分類: JavaJSF
          主站蜘蛛池模板: 茂名市| 乐都县| 左云县| 新宁县| 兴义市| 惠安县| 曲阳县| 巴楚县| 巧家县| 德化县| 民乐县| 富平县| 庐江县| 聊城市| 东城区| 万年县| 阿拉尔市| 枣庄市| 江华| 莱州市| 中方县| 神木县| 喀喇| 明溪县| 瓦房店市| 无棣县| 长宁区| 鄂伦春自治旗| 喀喇| 瑞丽市| 尼玛县| 兴国县| 唐河县| 西平县| 黄浦区| 陈巴尔虎旗| 台南市| 黔西县| 合肥市| 泸水县| 浮山县|