0

嗨,我真的可以在这方面提供一些帮助,因为我无法让按钮选择 .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') 不是:(。

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

4

1 回答 1

2

jsBin 演示

添加第 11 行 - 并在您的 HTML 中$(this)缺少:</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>
于 2012-08-26T18:51:10.257 回答