Плагин Jquery chechboxTree и Cookie - проблема с получением детей - PullRequest
0 голосов
/ 16 ноября 2010

Я использую плагин checkboxTree в связи с плагином cookie для jquery.Моя проблема в том, что я хотел бы установить / снять все куки-файлы дочерних элементов (из заголовка), когда был установлен флажок с классом «заголовок».Как я могу это сделать?Вот мой код .js:

 // 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-код:

   <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>

1 Ответ

0 голосов
/ 16 ноября 2010
  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);
      }
    });
  }

И удалите "," в конце

expandImage: '../media/images/icons/plus.png',
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...