简体   繁体   English

当我使用jQuery单击选择按钮时,如何选择所有复选框

[英]How do I select all check box when I click on Select button using jQuery

I have HTML page which have multiple check boxes and individually they can be checked. 我有HTML页面,其中有多个复选框,可以单独检查它们。 I have button select , so what I am suppose to do is. 我有按钮select ,所以我想做的是。 When I click on select all the check boxes should get selected, and deselected. 当我单击全选时,所有复选框应被选中,然后取消选中。

Html HTML

 <html> <head> <script src="jquery.js"></script> </head> <body> <script> $(document).ready(function() { $('#selectAll').click(function(event) { //on click if(this.checked) { // check select status $('.btn-chk').each(function() { //loop through each checkbox this.checked = true; //select all checkboxes with class "checkbox1" }); }else{ $('.btn-chk').each(function() { //loop through each checkbox this.checked = false; //deselect all checkboxes with class "checkbox1" }); } }); }); </script> <table id="example" cellspacing="0" width="20%"> <thead> <tr> <th><button type="button" id="selectAll" class="myClass">Select</button></th> <th>number</th> <th>company</th> <th> Type</th> <th> Address</th> <th>Code</th> </tr> </thead> <tbody> <tr> <td><span class="button-checkbox"> <button type="button" class="btn-chk" data-color="success"></button> <input type="checkbox" class="hidden" /> </span> </td> <td>1</td> <td>APPLE</td> <td>IT</td> <td>San Jones</td> <td>US</td> </tr> <tr> <td><span class="button-checkbox"> <button type="button" class="btn-chk" data-color="success"></button> <input type="checkbox" class="hidden" /> </span> </td> <td>2</td> <td>DELL</td> <td>IT</td> <td>San Jones</td> <td>US</td> </tr> <tr> <td><span class="button-checkbox"> <button type="button" class="btn-chk" data-color="success"></button> <input type="checkbox" class="hidden" /> </span> </td> <td>3</td> <td>Amazon</td> <td>IT</td> <td>San Jones</td> <td>US</td> </tr> <tr> <td><span class="button-checkbox"> <button type="button" class="btn-chk" data-color="success"></button> <input type="checkbox" class="hidden" /> </span> </td> <td>4</td> <td>Microsoft</td> <td>IT</td> <td>San Jones</td> <td>US</td> </tr> </tbody> </body> </html> 

Output 输出量

在此处输入图片说明

In the image I have select button. 在图像中,我有选择按钮。 what I want is when I click on select button all the check boxes should get selected 我想要的是当我单击选择按钮时,所有复选框都应该被选中

If you want to toggle the checkbox state, you can do like this 如果要切换复选框状态,可以这样

var chkd = true;
$('#selectAll').click(function(event) {
    $(":checkbox").prop("checked", chkd);
    chkd = !chkd;
});

Fiddle 小提琴

#selectAll is a button, it doesn't have a checked property, but one could emulate that with a data attribute instead. #selectAll是一个按钮,它没有checked属性,但是可以使用data属性来模拟它。

Secondly, .btn-chk isn't a checkbox either, so it can't be checked, you have to target the checkboxes 其次, .btn-chk也不是一个复选框,因此无法选中它,您必须定位该复选框

$(document).ready(function() {
    $('#selectAll').click(function(event) {
        var checked = !$(this).data('checked');

        $('.btn-chk').next().prop('checked', checked);

        $(this).data('checked', checked);
    });

});

FIDDLE 小提琴

An simple way is like below: 一个简单的方法如下所示:

$(function() {
    $('#selectAll').click(function() {
        $(':checkbox').prop('checked', !$(':checkbox').prop('checked'));
    });
});

The Demo. 演示。

Check this jsFiddle 检查这个jsFiddle

$(document).ready(function() {
   $('#selectAll').click(function() {
        $(':checkbox').prop('checked', !$(':checkbox').prop('checked'));
    });
});

You can simply use it For more info on jQuery visit w3schools-jquery 您可以简单地使用它。有关jQuery的更多信息,请访问w3schools-jquery

Try this- 尝试这个-

var checked = true;
$('#selectAll').click(function(event) {
    $('table tr').each(function( index ) {
        $(this).find('input[type="checkbox"]').prop(checked, true);
    });
    checked = !checked;
});

Change your jQuery code to 将您的jQuery代码更改为

 $('#selectAll').click(function(event) {  //on click 
    if($('.hidden').prop('checked') == false) { // check select status
        $('.hidden').each(function() { //loop through each checkbox
            this.checked = true;  //select all checkboxes with class "checkbox1"               
        });
    }else{
        $('.hidden').each(function() { //loop through each checkbox
            this.checked = false; //deselect all checkboxes with class "checkbox1"                       
        });         

    }
});

This will toggle between checked and unchecked of the checkboxes when you click the button . 这将切换之间的checkedunchecked的的checkboxes当你点击button

inside the "click" function "this" is button and not a checkbox. 在“点击”功能中,“此”是按钮,而不是复选框。 and button property checked is underfined. 和按钮属性检查不足。 "$('.btn-chk').each" - is loop on each element with class ".btn-chk", in your HTML is buttons and not an checkboxes. “ $('。btn-chk')。each”-在类为“ .btn-chk”的每个元素上循环,在您的HTML中是按钮而不是复选框。

in your context the "#selectAll" must be a checkbox and put class '.btn-chk' on your checkboxes 在您的上下文中,“#selectAll”必须是一个复选框,并将类“ .btn-chk”放在您的复选框中

You can do it like this also with ':checkbox' selector. 您也可以使用':checkbox'选择器来做到这一点。

$(document).ready(function () {

    $('#selectAll').on('click', function () {
        $(':checkbox').each(function () {

            $(this).click();
        });
    });
});

This will definitely works. 这绝对可以。

$("#selectAll").click(function(){
    var checked = !$(this).data('checked');
    $('.btn-chk').prop('checked', checked);         
    $(this).data('checked', checked);
});

暂无
暂无

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

相关问题 当我使用select2 jquery选择&#39;ALL&#39;项目时,如何禁用选择框中的所有项目 - How to disable all items in select box when i am selecting 'ALL' item using select2 jquery 如何使用jQuery选择并单击 - How do I select and click using jQuery 如何使用jQuery忽略发送到服务器的元素列表中的“全选”复选框? - How do I use jQuery to omit the “Select All” check box from the list of elements sent to the server? 我如何 select 所有 p 标签并在单击复选框时使它们可见? - How do I select all p tags and make them visible when check box is clicked? 使用JavaScript或jQuery,如何检查选择框是否与原始值匹配? - Using JavaScript or jQuery, how do I check if select box matches original value? 如果用户未选中复选框或单选按钮,如何为复选框或单选按钮设置一些默认值 - how do I set some default value for a check box or a radio button if user do not check the check box or select a button 如何选择按钮单击上的复选框 - How to select the check box on button click 使用d3-如何在单击按钮时从数组中选择特定数据以突出显示? - Using d3 - How do I select specific data from array to highlight when I click a button? 如何使用jquery选择“向上钻取”按钮,以便可以执行on click事件? - How to select the drillup button using jquery, so I can do an on click event? 如何选择包含的所有文本 <pre> 使用jQuery? - How do I select all text contained in <pre> using jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM