javascript jquery统一选择和取消选择所有复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14729286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:25:37  来源:igfitidea点击:

jquery uniform select and deselect all checkbox

javascriptjquery

提问by Nilesh patel

I have problem with select all / deselect checkbox

我在全选/取消选择复选框时遇到问题

Code Here

代码在这里

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

When I select checkbox(for all check box true) jQuery add span tag with class="checked"

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

     spanTag.addClass(options.checkedClass);

And no one check box are active.

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

<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>

采纳答案by Nishant Jani

Are you aiming for something like this ?

你的目标是这样的吗?

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

See this Demo

看这个演示

回答by MG_Bautista

Try this...

试试这个...

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

OR for the las version.

或 las 版本。

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

See this Example...

看到这个例子...

回答by AndRaGhu

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

this is code is correctly working for the first time only. For the second time if i click the check all check box it is not working. It only got checked.

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

回答by Robert Benyi

Simplest solution call uniform.update:

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

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

回答by Ajay Kumar

<script>
$('document').ready(function(){

    $('#selectAll').click(function(){
        if($(this).is(':checked')){
            $('#uniform-undefined span').attr("class","checked");
        }
        else{
            $('#uniform-undefined span').attr("class","");
        }
    })
});
</script>