mardi 31 janvier 2017

Check and uncheck checkboxes after click one main checkbox in jQuery

This is not a duplicate of any question.I tried Stackoverflow answers but those are not helpful for me.

I have one Select All Checkbox.If it checked all other check boxes are checked, if I unchecked it all other check-boxes are unchecked.That's my task.

I did it and its working.but sometimes its not working properly. Please check my codes and give some idea to develop it.

HTML

<th width="10%" style="background-color: #cce5ff;"><input type="checkbox" id="<?php echo $user['user']->id; ?>" class="checkbox select_all user_<?php echo $user['user']->id; ?>" style="cursor: pointer;"></th>

<td width="10%">
    <input type="checkbox" id="view_<?php echo $folder->id; ?>_<?php echo $user['user']->id; ?>" class="is_check_v check_this_<?php echo $user['user']->id; ?>" value="view" <?php if(isset($user['permission'])){if(!empty($user_permissions) && $user_permissions->can_view==1){ echo 'checked';}else{echo '';}}?>  name="per">     

    <input type="hidden" id="h_view_<?php echo $folder->id; ?>_<?php echo $user['user']->id; ?>" name="view_permission[][<?php echo $folder->id; ?>][<?php echo $user['user']->id; ?>]" value="<?php if(!empty($user_permissions)){ echo $user_permissions->can_view;}else{ echo 0; } ?>">
            </td>
            <td width="10%">
                <input type="checkbox" id="edit_<?php echo $folder->id; ?>_<?php echo $user['user']->id; ?>" class="is_check_e check_this_<?php echo $user['user']->id; ?>" value="edit" <?php if(isset($user['permission'])){if(!empty($user_permissions) && $user_permissions->can_edit==1){ echo 'checked';}else{echo '';}}?> name="per">
                <input type="hidden" id="h_edit_<?php echo $folder->id; ?>_<?php echo $user['user']->id; ?>" name="edit_permission[][<?php echo $folder->id; ?>][<?php echo $user['user']->id; ?>]" value="<?php if(!empty($user_permissions)){ echo $user_permissions->can_edit;}else{ echo 0; } ?>">
            </td>
            <td width="7%">
                <input type="checkbox" id="delete_<?php echo $folder->id; ?>_<?php echo $user['user']->id; ?>" class="is_check_d check_this_<?php echo $user['user']->id; ?>" value="delete" <?php if(isset($user['permission'])){if( !empty($user_permissions) && $user_permissions->can_delete==1){ echo 'checked';}else{echo '';}}?> name="per">
                <input type="hidden" id="h_delete_<?php echo $folder->id; ?>_<?php echo $user['user']->id; ?>" name="delete_permission[][<?php echo $folder->id; ?>][<?php echo $user['user']->id; ?>]" value="<?php if(!empty($user_permissions)){ echo $user_permissions->can_delete;}else{ echo 0; } ?>">
            </td>

jQuery

$(".select_all").click(function () {

        user_id = $(this).attr('id');

        $.each($('.check_this_'+user_id),function(){
            $(this).prop("checked", $('.user_'+user_id).prop("checked"));                

            $(this).click(function(){
                $('.user_'+user_id).prop("checked", false);           
            });

        });

    });




Aucun commentaire:

Enregistrer un commentaire