首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery chechboxTree和Cookie插件-获取孩子的问题

Jquery chechboxTree和Cookie插件-获取孩子的问题
EN

Stack Overflow用户
提问于 2010-11-16 22:13:41
回答 1查看 281关注 0票数 0

我使用的checkboxTree插件与jquery的cookie插件相关。我的问题是,当一个带有类"header“的复选框被点击时,我想设置/取消设置子对象的所有cookie(来自header)。我该怎么做呢?下面是我的.js代码:

代码语言:javascript
复制
 // When page has loaded, check each checkbox if it's checked (set cookie) or not (unset cookie)
 $(".tree input:checkbox").each(function(){
  var elementId = this.id;

  // Check if a cookie of the element exists
  if($.cookie(elementId)){
   // If so, change attribute to checked
   $(this).attr("checked", "checked");
  }else{
   // If not, change attribute to not checked
   $(this).attr("checked", "");
  }

 });

 $(".tree input:checkbox").change(function(){
  if($(this).attr("class") == "header"){
   if($(this).is(":checked")){
    setCookie(this.id);
    // Set cookie for all children
   }else{
    unsetCookie(this.id);
    // Unset cookie for all children
   }
  }else{
   if($(this).is(":checked")){
    setCookie(this.id);
   }else{
    unsetCookie(this.id);
   }
  }
 });

 function setCookie(element){
  $.cookie(element, "checked");
 }

 function unsetCookie(element){
  $.cookie(element, null);
 }


    $('.tree').checkboxTree({
     onCheck: { 
         node: 'expand' 
        }, 
        onUncheck: { 
            node: 'collapse' 
        }, 
     collapseImage: '../media/images/icons/minus.png',
        expandImage: '../media/images/icons/plus.png'
    });

下面是我的html代码:

代码语言:javascript
复制
   <div id="filterTree">
     <ul class="tree">
         <li><input type="checkbox" id="Human Ressources" class="header">Human Resources
             <ul>
                 <li><input type="checkbox" id="Finance">Finance
                 <li><input type="checkbox" id="Personnel">Personnel
                 <li><input type="checkbox" id="Post Office">Post Office
                 <li><input type="checkbox" id="House Service">House Service
                 <li><input type="checkbox" id="Reception">Reception
                </ul>
         </ul>
    </div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-11-16 22:51:30

代码语言:javascript
复制
  if($(this).attr("class") == "header"){
    //selects all input of the type checkbox that are within the ul li elements, each loops trough the list of those elements
    $(this).parents("ul.tree").find("ul li input:checkbox").each(function() {
      if($(this).is(":checked")){  //if this item of the list is checked set a cookie, otherwise unset it.
        setCookie(this.id);
      }
      else {
        unsetCookie(this.id);
      }
    });
  }

删除末尾的",“

代码语言:javascript
复制
expandImage: '../media/images/icons/plus.png',
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4195015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档