3

我对全选/取消选择复选框有疑问

代码在这里

<input type="checkbox" value="" onclick="checkedAll();" name="checkall" id="checkall"/>

  function checkedAll() {
      $('.all span').click(function () {
       if(document.getElementById("checkall").checked == true)
       {
         $('#uniform-undefined span').addClass('checked');      
       }
       else
       {
         $('#uniform-undefined span').removeClass('checked');
       }
     });
     $.uniform.update();
   }

当我选择复选框(所有复选框为真)时,jQuery 添加 span 标签class="checked"

     spanTag.addClass(options.checkedClass);

并且没有一个复选框处于活动状态。

<li><label><div id="uniform-undefined" class="checker">">
   <span class="checked"><input type="checkbox" value="F2" style="opacity: 0;"></span>
   </div><b>1 </b>
 </label></li>

<li><label><div id="uniform-undefined" class="checker">
   <span class="checked"><input type="checkbox" value="F2" style="opacity: 0;"></span>
   </div><b>2 </b>
</label></li>

<li><label><div id="uniform-undefined" class="checker">
    <span class="checked"><input type="checkbox" value="F3" style="opacity: 0;"></span>
    </div><b>3 </b>
</label></li>
4

5 回答 5

3

试试这个...

  $('#all').on('click', function(){
        $(':checkbox').attr("checked",$(this).is(':checked'));
  });

或者对于 las 版本。

  $('#all').on('click', function(){
        $(':checkbox').prop("checked",$(this).is(':checked'));
  });

请参阅此示例...

于 2013-02-06T13:02:29.470 回答
2

你的目标是这样的吗?

$('document').ready(function(){
    $('#all').click(function(){
        if($(this).is(':checked')){
            $('.group').attr("checked",true);
        }
        else{
            $('.group').attr("checked",false);
        }
    })
});

看这个演示

于 2013-02-06T12:54:11.510 回答
0
$('document').ready(function(){
    $('#all').click(function(){
        if($(this).is(':checked')){
            $('.group').attr("checked",true);
        }
        else{
            $('.group').attr("checked",false);
        }
    })
});

这是代码仅第一次正确工作。如果我第二次单击选中所有复选框,则它不起作用。它只是被检查了。

于 2013-04-08T07:06:33.733 回答
0

最简单的解决方案调用uniform.update:

        $(".all span").each(function () {
            $(this).prop("checked", document.getElementById("checkall").checked );
        });
        $.uniform.update();
于 2015-12-17T10:27:18.627 回答
-2
<script>
$('document').ready(function(){

    $('#selectAll').click(function(){
        if($(this).is(':checked')){
            $('#uniform-undefined span').attr("class","checked");
        }
        else{
            $('#uniform-undefined span').attr("class","");
        }
    })
});
</script>
于 2015-07-08T11:09:23.247 回答