简体   繁体   English

Jquery - 检查页面加载时的所有复选框

[英]Jquery - Check all checkboxes on page load

I know this may seem like an amateur problem but I am having trouble selecting all the checkboxes on page load using Jquery. 我知道这可能看起来像一个业余问题,但我在使用Jquery选择页面加载时的所有复选框时遇到问题。 I figured it out with pure JS. 我用纯JS来解决它。

Also, once that problem is solved I need to iterate through all checkboxes and see if they are all checked. 此外,一旦该问题得到解决,我需要遍历所有复选框,看看它们是否全部被检查。 If they are, alert the user they are all selected, if not all of them are selected then alert the user another message. 如果是,则提醒用户他们都被选中,如果不是全部被选中,则提醒用户另一条消息。

I have put together a simple Jsfiddle to help get started. 我已经整理了一个简单的Jsfiddle以帮助您入门。 Any help would be appreciated. 任何帮助,将不胜感激。

JSFIDDLE 的jsfiddle

<table>
<tr>
    <th>Days of Week</th>
        <td>
        <div class="checkbox-group" id="checkboxes">
             <ul>
             <li>
                <input type="checkbox" id="mon"/>
                <label for="mon">MON</label>
             </li>
             <li>
                <input type="checkbox" id="tue"/>
                <label for="tue">TUE</label>
             </li>
             <li>
                <input type="checkbox" id="wed"/>
                <label for="wed">WED</label>
             </li>
             <li>
                <input type="checkbox" id="thur"/>
                <label for="thur">THUR</label>
             </li>
             <li>
                <input type="checkbox" id="fri"/>
                <label for="fri">FRI</label>
             </li>
             <li>
                <input type="checkbox" id="sat"/>
                <label for="sat">SAT</label>
             </li>
             <li>
                <input type="checkbox" id="sun"/>
                <label for="sun">SUN</label>
             </li>
             </ul>              
            </div>
 <span id="allChecked" style="display:block; width:425px; text-align:center; color:#999999;">
                (all days selected)
            </span>
        </td>
    </tr>

$( document ).ready( function(){
    var checkboxes = $( ':checkbox' );

    // Check all checkboxes
    checkboxes.prop( 'checked', true );

    // Check if they are all checked and alert a message
    // or, if not, alert something else.
    if ( checkboxes.filter( ':checked' ).length == checkboxes.length )
        alert( 'All Checked' );
    else
        alert( 'Not All Checked' );
});

JSFIDDLE 的jsfiddle

or if you want to update the span : 或者如果您想更新span

$( document ).ready( function(){
    var checkboxes = $( ':checkbox' ),
        span       = $( '#allChecked' );

    checkboxes.prop( 'checked', true );

    checkboxes.on( 'change', function(){
        var checked = checkboxes.filter( ':checked' );
        if ( checked.length == checkboxes.length )
            span.text( '(All Days Selected)' );
        else
        {
            var days = jQuery.map( checked, function(n){return n.id;} );
            span.text( '(' + days.join(', ') + ' Selected)' );
        }
    } );
});

JSFIDDLE 的jsfiddle

To check them all 检查一下

$('[type="checkbox"]').attr('checked', true);

to see if they are all checked (seems you have to set the attribute for the custom styling) 看看它们是否都被选中(似乎你必须设置自定义样式的属性)

$('[type="checkbox"]').length === $('[type="checkbox"]:checked').length

FIDDLE 小提琴

$(document).ready(function(){
   $(':checkbox').attr('checked',true);
});

I just tried it in your JSFiddle link and it works. 我刚刚在你的JSFiddle链接中尝试过它,它可以工作。

Please go through the link below that shows various ways to achieve the same: 请点击下面的链接,其中显示了实现相同目标的各种方法:

Check all checkboxes on page load with jQuery 使用jQuery检查页面加载的所有复选框

Hope this helps. 希望这可以帮助。 Thanks, Jatin 谢谢,贾丁

$(function(){
$('[type=checkbox]').attr({'checked':'checked'});

var checkedArray = [] ; 
$.each($('[type= checkbox]'),function(index,value){
    if($(this).attr('checked') == 'checked')
    checkedArray.push($(this).attr('id'));
});
    console.log(checkedArray.length);
    //["mon", "tue", "wed", "thur", "fri", "sat", "sun"] 
});

DEMO DEMO

checkedArray array will have a list of id of the checked checkbox checkedArray数组将包含已选中复选框的id列表

And you can do checkedArray.length to get the array length 你可以使用checkedArray.length来获取数组长度

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM