开关复选框的选中状态

434

我有以下内容:

$(document).ready(function()
{
    $("#select-all-teammembers").click(function() {
        $("input[name=recipients\\[\\]]").attr('checked', true);
    });                 
});

当点击 id="select-all-teammembers" 时,我希望它可以在选中和未选中之间切换。有什么简洁的代码实现方式吗?

24个回答

2
如果你想单独切换每个框(或者只需要一个框也可以):
我建议使用 .each(),因为它很容易修改,如果你希望发生不同的事情,并且相对较短且易于阅读。
例如:
// toggle all checkboxes, not all at once but toggle each one for its own checked state:
$('input[type="checkbox"]').each(function(){ this.checked = ! this.checked });

// check al even boxes, uncheck all odd boxes:
$('input[type="checkbox"]').each(function(i,cb){ cb.checked = (i%2 == 0); });

// set all to checked = x and only trigger change if it actually changed:
x = true;
$('input[type="checkbox"]').each(function(){
    if(this.checked != x){ this.checked = x; $(this).change();}  
});

顺便说一下... 我不确定为什么每个人都使用.attr()或.prop() 来(取消)勾选事项。

据我所知,在所有浏览器中,element.checked始终起着相同的作用?


2

你可以简单地使用这个

$("#chkAll").on("click",function(){
    $("input[name=checkBoxName]").prop("checked",$(this).prop("checked"));
});

2

在某些情况下,全选复选框应该自动更新本身。试着点击'#select-all-teammembers',然后取消几个项目的选择,再次点击全选。你会发现不一致性。为了防止这种情况,可以使用以下技巧:

  var checkBoxes = $('input[name=recipients\\[\\]]');
  $('#select-all-teammembers').click(function() {
    checkBoxes.prop("checked", !checkBoxes.prop("checked"));
    $(this).prop("checked", checkBoxes.is(':checked'));
  }); 

顺便说一下,所有复选框的DOM对象应该像上面描述的那样被缓存。


2
假设需要通过图像来切换复选框,以下方法适用:
<img src="something.gif" onclick="$('#checkboxid').prop('checked', !($('#checkboxid').is(':checked')));">
<input type="checkbox" id="checkboxid">

4
因为这是您的第一个回答,所以请注意最好遵循发布者在问题中使用的模式 - 如将jQuery代码放在文档就绪处理程序中而不是嵌入在标记中。如果您有不这样做的理由,请添加解释。 - Mark Schultheiss

2

The most basic example would be:

// get DOM elements
var checkbox = document.querySelector('input'),
    button = document.querySelector('button');

// bind "cilck" event on the button
button.addEventListener('click', toggleCheckbox);

// when clicking the button, toggle the checkbox
function toggleCheckbox(){
  checkbox.checked = !checkbox.checked;
};
<input type="checkbox">
<button>Toggle checkbox</button>


1
这段代码将在单击任何用于网页模板的切换开关动画器时切换复选框。请根据您的代码替换“.onoffswitch-label”。这里切换的复选框是“checkboxID”。
$('.onoffswitch-label').click(function () {
if ($('#checkboxID').prop('checked')) 
 {
   $('#checkboxID').prop('checked', false);
 }
else 
 {
   $('#checkboxID').prop('checked', true);
 }
});

1
这个对我非常有效。
   $("#checkall").click(function() {
       var fruits = $("input[name=fruits\\[\\]]");
        fruits.prop("checked", $(this).prop("checked"));
    });

1
你知道你正在给"checked"属性赋予它已经具有的完全相同的值,对吧?这不是有点尴尬吗?显然你想在它之前放一个!符号。 - vsync

1

您也可以通过以下方式切换复选框。当前使用bootstrap切换复选框。

<link href='https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css' rel='stylesheet'>
<script src='https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js'></script>

<input type='checkbox' id='toggleScheduler' name='toggleScheduler' data-toggle='toggle'  data-on='Enabled'  data-off='Disabled'

$("#toggleScheduler").bootstrapToggle('on'); // enable toggle checkbox
$("#toggleScheduler").bootstrapToggle('off'); // disable toggle checkbox

1
jQuery("#checker").click(function(){
    jQuery("#mydiv :checkbox").each(function(){
        this.checked = true;
    });
});
jQuery("#dechecker").click(function(){
    jQuery("#mydiv :checkbox").each(function(){
        this.checked = false;
    });
});
jQuery("#checktoggler").click(function(){
    jQuery("#mydiv :checkbox").each(function(){
        this.checked = !this.checked;
    });
});

;)


1
一种更好的方法和用户体验
$('.checkall').on('click', function() {
   var $checks  = $('checks');
   var $ckall = $(this);

    $.each($checks, function(){
        $(this).prop("checked", $ckall.prop('checked'));
    });
});

$('checks').on('click', function(e){
   $('.checkall').prop('checked', false);
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接