2012-01-01 78 views
2

当您点击按钮时,列表元素内的所有复选框应该处于活动状态。但通过点击按钮,按钮自行消失。 我做错了什么? 这里是我的代码:按钮消失,通过点击它启用复选框

这里的JavaScript:

$('.filter-arrow').click(function() { 
     //get id from clicked element 
     var id = $(this).attr("id"); 

     //switch class to display open arrow 
     $(this).toggleClass("arrow-close"); 

     //get function to enable checkbox 
     checkbox($('.parentcheck'), $(this)) 
}); 
function checkbox(check, arrow){ 
    if(arrow.attr("class","checkAll")){ 
     for(var i=0; i<check.length; i++){ 
      check[i].checked = true; 
      var arrow_id = arrow.attr("id"); 
      arrow_id.toggleClass("uncheckAll"); 
     } 
    }else{ 
     for(var i=0; i<check.length; i++){ 
      check[i].checked = false; 
      var arrow_id = arrow.attr("id"); 
      arrow_id.toggleClass("checkAll"); 
     } 
    } 
} 

这里的HTML:

<ul> 
    <li class="checkbox"> 
      <!--Button--> 
      <div id="first-arrow" class="filter-arrow checkAll arrow-open"></div> 
      <input id="check" class="parentcheck" name="parentcheck" type="checkbox"/> 
      <p>first-element</p> 
      <!-- begin sub content --> 
      <ul> 
       <li class="checkbox"> 
        <div id="first-sub-arrow" class="arrow-open"></div> 
        <input class="input-checkbox" name="checkbox" type="checkbox"/> 
        <p>first-sub-element</p> 
        <!-- begin subsub content --> 
        <ul> 
         <li class="checkbox "> 
         <input class="checkbox" type="checkbox" name="subcheckbox" /> 
         <p>first-sub-sub-element</p> 
         </li> 
         <li> 
         <input class="checkbox" type="checkbox" name="subcheckbox" /> 
         <p> first-sub-sub-element(2)</p> 
         </li> 
        </ul> 
       </li> 
      </ul> 
    </li> 
    <li> 
      … 
    </li> 
</ul> 

CSS:

.filter-arrow{ 
    clear:both; 
    float:right; 
    height: 12px; 
    width: 22px; 
}  

.arrow-open{ 
     background:url(pic/arrow-close.png) no-repeat left top; 
    } 

    .arrow-close{ 
     background: url(pic/arrow-open.png) no-repeat left top; 
    } 

现在它的工作...林这样做没有课堂“ch eckAll”和‘uncheckAll’ 现在我不会用的按钮,点击做到这一点 - 只是父复选框cklicking ...

就像这样:

var filterGeschichte; 

filterGeschichte = function($filterContainer,$categoryToggler){ 

    $categoryToggler.click(function(e){ 
     var $toggler = $(this); 
     $toggler.nextAll("ul.filter-sub").eq(0).slideToggle(300).find("li").show(); 

    }); 
    $filterContainer.each(function(){ 
     $(this).find("input[type=checkbox]").change(function(){ 
      var $that = $(this); 

      //console.log($that.is(":checked")) 

      if($that.parent().find("ul.filter-sub").length) { 
       $that.parent().find("ul.filter-sub").find("input[type=checkbox]").attr("checked", $that.is(":checked")); 
      } 
     }); 
    }); 
}; 

// JavaScript Document 
$(document).ready(function(){ 

filterGeschichte($("#scrollthefilter form:eq(0)"), $("div.filter-arrow")) 
+0

按钮在哪里?在代码中看不到它 – gotofritz 2012-01-01 13:06:30

+2

请添加一个链接到一个jsfiddle测试 – Mbrevda 2012-01-01 13:06:48

+0

是的,似乎带了一半的代码,请检查。 – 2012-01-01 13:07:21

回答

1

检查所有复选框应像这样简单:

$('.first-arrow').click(function() { 
    $(":checkbox").attr('checked', 'checked'); 
}); 
+0

您可以更新您的代码以使用我的方法吗?我上面的三行不隐藏按钮... – 2012-01-01 16:36:49