Jquery按钮选择div中的所有复选框和按钮选择页面上的全部

时间:2012-08-26 18:46:18

标签: javascript jquery checkbox

嗨我真的可以帮忙解决这个问题,因为我无法通过按钮来选择.voltSiteResultContainer中的所有复选框来工作

HTML示例:

 <div class="pageContainer">
<input type="button" class="check" value="check all"> // want this button to check all on page
<div class="voltSiteResultContainer">
    <div class="checkWrap">
        <input type="button" class="checkSite" value="check all"> // want this button to check all within .voltSiteResultContainer
    </div>
    <input name="checkFile" type="checkbox" class="checkFile" value="1514583">
    <input name="checkFile" type="checkbox" class="checkFile" value="1514583">
    <input name="checkFile" type="checkbox" class="checkFile" value="1514583">
</div>
<div class="voltSiteResultContainer">
    <div class="checkWrap">
        <input type="button" class="checkSite" value="check all"> // want this button to check all within .voltSiteResultContainer
    </div>
    <input name="checkFile" type="checkbox" class="checkFile" value="1514583">
    <input name="checkFile" type="checkbox" class="checkFile" value="1514583">
    <input name="checkFile" type="checkbox" class="checkFile" value="1514583">
</div>

当前Jquery:

 <script type="text/javascript">
            $(document).ready(function(){
        $('.check:button').toggle(function(){
            $('input:checkbox').attr('checked','checked');
            $(this).val('uncheck all')
        },function(){
            $('input:checkbox').removeAttr('checked');
            $(this).val('check all');        
        })
    })
            $(document).ready(function(){
        $('.checkSite:button').toggle(function(){
            .closest('.voltSiteResultContainer').find('input:checkbox').attr('checked','checked');
            $(this).val('uncheck all')
        },function(){
            $('input:checkbox').removeAttr('checked');
            $(this).val('check all');        
        })
    })

所以$('。check:button')工作,但$('。checkSite:button')不是:(。

提前感谢您提供给我的任何帮助:)

1 个答案:

答案 0 :(得分:2)

jsBin demo

在第11行添加$(this) - 并在HTML中添加</div>

 $(document).ready(function(){

        $('.check:button').toggle(function(){
            $('input:checkbox').attr('checked','checked');
            $(this).val('uncheck all');
        },function(){
            $('input:checkbox').removeAttr('checked');
            $(this).val('check all');        
        });

        $('.checkSite:button').toggle(function(){
            $(this).closest('.voltSiteResultContainer').find('input:checkbox').attr('checked','checked'); // HERE!!
            $(this).val('uncheck all');
        },function(){
            $(this).closest('.voltSiteResultContainer').find('input:checkbox').removeAttr('checked');
            $(this).val('check all');        
        });

});

修正了HTML标记:

<div class="pageContainer">
    <input type="button" class="check" value="check all" /> // want this button to check all on page
    <div class="voltSiteResultContainer">
      <div class="checkWrap">
         <input type="button" class="checkSite" value="check all" /> // want this button to check all within .voltSiteResultContainer
      </div>
      <input name="checkFile" type="checkbox" class="checkFile" value="1514583" />
      <input name="checkFile" type="checkbox" class="checkFile" value="1514583" />
      <input name="checkFile" type="checkbox" class="checkFile" value="1514583" />
    </div>  
</div>

<div class="voltSiteResultContainer">
    <div class="checkWrap">
      <input type="button" class="checkSite" value="check all" /> // want this button to check all within .voltSiteResultContainer
    </div>
   <input name="checkFile" type="checkbox" class="checkFile" value="1514583" />
   <input name="checkFile" type="checkbox" class="checkFile" value="1514583" />
   <input name="checkFile" type="checkbox" class="checkFile" value="1514583" />
</div>