lundi 1 juin 2015

JQuery get checkboxes groups as arrays

Here is the problem :

I use jquery 2.x, I need to retrieve data as array on event (click).

I have several checkboxes, These checkboxes represent some permissions for some users.

Here is what it looks like :

<label class="checkbox-inline"><input class="permission" checked="" value="1" data-id="4" data-type="view" type="checkbox">View</label>
<label class="checkbox-inline"><input class="permission" value="1" data-id="4" data-type="edit" type="checkbox">Edit</label>
<label class="checkbox-inline"><input class="permission" value="1" data-id="4" data-type="delete" type="checkbox">Delete</label>
<label class="checkbox-inline"><input class="permission" checked="" value="1" data-id="3" data-type="view" type="checkbox">View</label>
<label class="checkbox-inline"><input class="permission" checked="" value="1" data-id="3" data-type="edit" type="checkbox">Edit</label>
<label class="checkbox-inline"><input class="permission" value="1" data-id="3" data-type="delete" type="checkbox">Delete</label>

Data-id attribute represents the user id I am editing permissions for.

Data-type attribute represents the type of permission I am editing.

So, what I want to retrieve as array (that will be handled in php later) should look like this :

{
    {//Permissions for user id 3
        "id":3,
        "view":true,
        "edit":true,
        "delete":false
    },
    {//permissions for user id 4
        "id":4,
        "view":true,
        "edit":false,
        "edit":false
    }
}
//And more if there are more users/checkboxes on the page.

Thanks for reading/help




Aucun commentaire:

Enregistrer un commentaire