jquery uniform选择并取消选中所有复选框

时间:2013-02-06 12:37:40

标签: javascript jquery

我选择全部/取消选中复选框

时遇到问题

代码

<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();
   }

当我选中复选框时(对于所有复选框为true),jQuery使用class="checked"

添加span标记
     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>

5 个答案:

答案 0 :(得分:3)

试试这个......

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

对于las版本。或

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

请参阅此Example ...

答案 1 :(得分:2)

你的目标是什么?

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

请参阅此Demo

答案 2 :(得分:0)

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

这是代码第一次正常工作。如果我第二次点击全部勾选复选框,它将无法正常工作。它只被检查了。

答案 3 :(得分:0)

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

        $(".all span").each(function () {
            $(this).prop("checked", document.getElementById("checkall").checked );
        });
        $.uniform.update();

答案 4 :(得分:-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>