jeudi 14 avril 2016

how to operate checkboxes with all conditions in jquery

I want a jquery condition which will handle the following scenarios -

1) Some of the checkboxes selected

2) All of the checkboxes selected

I want them both in a single loop. Currently, I've the following code to select all checkboxes only -

$('.check-all').click(function() {
    var selector = $(this).is(':checked') ? ':not(:checked)' : ':checked';
    var ids = [];

    $('#chkall input[type="checkbox"]' + selector).each(function() {
        $(this).trigger('click');
        ids.push($(this).attr('value'));
    });
    var id = [];
    id = JSON.stringify(ids);
    document.cookie = "ids="+id;
});

And taking checkboxes in my PHP code as -

echo '<ul class="list-unstyled" id="chkall">';
foreach ($contacts as $contact) {
    echo '<li>
    <div class="checkbox" id="checkboxes">
        <label><input type="checkbox" class="case" name="case" value="'.$contact['cust_id'].'" id="'.$contact['cust_id'].'">'.$contact['cust_fname'].' '.$contact['cust_lname'].'</label>
        <input type="hidden" value="'.$contact['cust_mobile'].'" id="'.$contact['cust_id'].'">
    </div>
</li>';

So, how should I handle both scenarios in a single loop ? I've send a cookie when checking is done.




Aucun commentaire:

Enregistrer un commentaire