简体   繁体   English

选中一个复选框后,选中所有其他复选框

[英]Check all other checkboxes when one is checked

I have a form and group of checkboxes in it. 我有一个表单和一组复选框。 (These checkboxes are dynamically created but I dont think it is important for this question). (这些复选框是动态创建的,但我认为这对于这个问题并不重要)。 The code that generates them looks like this (part of the form): 生成它们的代码看起来像这样(表单的一部分):

<div id="ScrollCB">
    <input type="checkbox" name="ALL" value="checked" checked="checked">
    All (if nothing selected, this is default) <br>
    <c:forEach items="${serviceList}" var="service">
       <input type="checkbox" name="${service}" value="checked"> ${service} <br>
    </c:forEach>
</div>

What I want to do is control, whether the checkbox labeled "ALL" is checked and if yes - check all other checkboxes (and when unchecked, uncheck them all). 我想要做的是控制,是否选中标记为“ALL”的复选框,如果是,则检查所有其他复选框(如果未选中,则取消选中所有复选框)。

I tried doing this with javascript like this (found some tutorial), but it doesnt work (and Im real newbie in javascript, no wonder): 我尝试用这样的javascript做这个(找到一些教程),但它不起作用(我在javascript中真正的新手,难怪):

<script type="text/javascript">
  $ui.find('#ScrollCB').find('label[for="ALL"]').prev().bind('click',function(){
  $(this).parent().siblings().find(':checkbox').attr('checked',this.checked).attr('disabled',this.checked);
}); });
</script>

Could you tell me some simple approach how to get it work? 你能告诉我一些简单的方法如何让它发挥作用? Thanks a lot! 非常感谢!

demo 演示

updated_demo updated_demo

HTML: HTML:

<label><input type="checkbox" name="sample" class="selectall"/> Select all</label>

<div id="checkboxlist">

    <label><input type="checkbox" name="sample[]"/>checkbox1</label><br />
    <label><input type="checkbox" name="sample[]"/>checkbox2</label><br />
    <label><input type="checkbox" name="sample[]"/>checkbox3</label><br />
    <label><input type="checkbox" name="sample[]"/>checkbox4</label><br />

</div>

JS: JS:

$('.selectall').click(function() {
    if ($(this).is(':checked')) {
        $('div input').attr('checked', true);
    } else {
        $('div input').attr('checked', false);
    }
});

HTML: HTML:

<form>
<label>
    <input type="checkbox" id="selectall"/> Select all
</label>
<div id="checkboxlist">
    <label><input type="checkbox" name="sample[]"/>checkbox1</label><br />
    <label><input type="checkbox" name="sample[]"/>checkbox2</label><br />
    <label><input type="checkbox" name="sample[]"/>checkbox3</label><br />
    <label><input type="checkbox" name="sample[]"/>checkbox4</label><br />
</div>
</form>

JS: JS:

$('#selectall').click(function() {
    $(this.form.elements).filter(':checkbox').prop('checked', this.checked);
});

http://jsfiddle.net/wDnAd/1/ http://jsfiddle.net/wDnAd/1/

Thanks to @Ashish, I have expanded it slightly to allow the "master" checkbox to be automatically checked or unchecked, if you manually tick all the sub checkboxes. 感谢@Ashish,我已稍微扩展它以允许自动选中或取消选中“master”复选框,如果您手动勾选所有子复选框。

FIDDLE 小提琴

HTML HTML

<label><input type="checkbox" name="sample" class="selectall"/>Select all</label>

<div id="checkboxlist">
    <label><input type="checkbox" class="justone" name="sample[]"/>checkbox1</label><br/>
    <label><input type="checkbox" class="justone" name="sample[]"/>checkbox2</label><br />
    <label><input type="checkbox" class="justone" name="sample[]"/>checkbox3</label><br />
    <label><input type="checkbox" class="justone" name="sample[]"/>checkbox4</label><br />
</div>

SCRIPT 脚本

$('.selectall').click(function() {
    if ($(this).is(':checked')) {
        $('input:checkbox').prop('checked', true);
    } else {
        $('input:checkbox').prop('checked', false);
    }
});

And now add this to manage the master checkbox as well... 现在添加它来管理主复选框...

$("input[type='checkbox'].justone").change(function(){
    var a = $("input[type='checkbox'].justone");
    if(a.length == a.filter(":checked").length){
        $('.selectall').prop('checked', true);
    }
    else {
        $('.selectall').prop('checked', false);
    }
});

Add extra script according to your checkbox group: 根据您的复选框组添加额外脚本:

<script language="JavaScript">
function selectAll(source) {
    checkboxes = document.getElementsByName('colors[]');
    for(var i in checkboxes)
        checkboxes[i].checked = source.checked;
}
</script>

HTML Code: HTML代码:

<input type="checkbox" id="selectall" onClick="selectAll(this,'color')" />Select All
<ul>
<li><input type="checkbox" name="colors[]" value="red" />Red</li>
<li><input type="checkbox" name="colors[]" value="blue" />Blue</li>
<li><input type="checkbox" name="colors[]" value="green" />Green</li>
<li><input type="checkbox" name="colors[]" value="black" />Black</li>
</ul>

use this i hope to help you i know that this is a late answer but if any one come here again 使用这个我希望能帮助你,我知道这是一个迟到的答案,但如果有人再来这里

$("#all").change(function () {
        $("input:checkbox").prop('checked', $(this).prop("checked"));
    });

You can use jQuery like so: 您可以像这样使用jQuery:

jQuery jQuery的

$('[name="ALL"]:checkbox').change(function () {
   if($(this).attr("checked")) $('input:checkbox').attr('checked','checked');
   else $('input:checkbox').removeAttr('checked');
});

A fiddle . 小提琴

I am not sure why you would use a label when you have a name on the checkbox. 我不知道为什么在复选框上有名字时会使用标签。 Use that as the selector. 使用它作为选择器。 Plus your code has no labels in the HTML markup so it will not find anything. 此外,您的代码在HTML标记中没有标签,因此它不会找到任何内容。

Here is the basic idea 这是基本的想法

$(document).on("click",'[name="ALL"]',function() {
    $(this).siblings().prop("checked",this.checked);
});

if there are other elements that are siblings, than you would beed to filter the siblings 如果有其他元素是兄弟姐妹,那么你就会过滤兄弟姐妹

$(document).on("click",'[name="ALL"]',function() {
    $(this).siblings(":checkbox").prop("checked",this.checked);
});

jsFiddle 的jsfiddle

var selectedIds = [];
function toggle(source) {
    checkboxes = document.getElementsByName('ALL');
    for ( var i in checkboxes)
        checkboxes[i].checked = source.checked;
}
function addSelects() {

    var ids = document.getElementsByName('ALL');

    for ( var i = 0; i < ids.length; i++) {
        if (ids[i].checked == true) {
            selectedIds.push(ids[i].value);
        }
    }
}

In HTML: 在HTML中:

Master Check box <input type="checkbox" onClick="toggle(this);"> 主复选框<input type="checkbox" onClick="toggle(this);">

Other Check boxes <input type="checkbox" name="ALL"> 其他复选框<input type="checkbox" name="ALL">

You can use the :first selector to find the first input and bind the change event to it. 您可以使用:first选择器查找第一个输入并将change事件绑定到它。 In my example below I use the :checked state of the first input to define the state of it's siblings. 在下面的例子中,我使用:第一个输入的:checked状态来定义它的兄弟姐妹的状态。 I would also suggest to put the code in the JQuery ready event. 我还建议将代码放在JQuery ready事件中。

    $('document').ready(function(){   
        $('#ScrollCB input:first').bind('change', function() {
            var first = $(this);
            first.siblings().attr('checked', first.is(':checked'));
        });
    });

Only in JavaScript with auto check/uncheck functionality of master when any child is checked/unchecked. 只有在JavaScript中,当选中/取消选中任何子项时,自动检查/取消选中master的功能。

function FnCheckAll()
    {
        var ChildChkBoxes = document.getElementsByName("ChildCheckBox");
        for (i = 0; i < ChildChkBoxes.length; i++)
        {
            ChildChkBoxes[i].checked = document.forms[0].CheckAll.checked;
        }
    }
function FnCheckChild()
    {
        if (document.forms[0].ChildCheckBox.length > document.querySelectorAll('input[name="ChildCheckBox"]:checked').length)
            document.forms[0].CheckAll.checked = false;
        else
            document.forms[0].CheckAll.checked = true;
    }

Master CheckBox: Master CheckBox:

<input type="checkbox" name="CheckAll" id="CheckAll" onchange="FnCheckAll()" />

Child CheckBox: 儿童复选框:

<input type="checkbox" name="ChildCheckBox" id="ChildCheckBox" onchange="FnCheckChild()" value="@employee.Id" />```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选中一个复选框后如何删除其他复选框上的复选框 - how to remove check on other checkboxes when one checkbox is checked 当组中的一个复选框被选中时,禁用并取消选中所有其他复选框 - When one checkbox is checked in a group, disable & uncheck all other checkboxes Javascript 在选中一个复选框时检查具有相同 ID 的所有复选框 - Javascript to check all checkboxes with the same ID when one is checked 选中所有复选框,如果选中了一个项目则反转 - check all checkboxes and reverse if one item is checked 触发检查所有复选框时,如何防止使用jQuery在dataTables内的其他页面上检查其他复选框 - When triggering to check all checkboxes, how do you prevent other checkboxes from being checked on other pages inside dataTables using jQuery 检查是否还选中了所有其他复选框; 工作,但我想要更多 - Check if all other checkboxes are checked; working but I want something more 检查在jQuery中检查的所有复选框 - Check all Checkboxes checked in jQuery 选中其他复选框时如何取消选中复选框? - How to uncheck checkboxes when check other one? 选中所有子复选框时如何检查父项 - How to check parent when all children checkboxes are checked 如何选中其下面的所有复选框后选中复选框? - How to check a checkbox when all the checkboxes below it are checked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM