mardi 16 janvier 2018

Check checkbox based on ajax response

I have a form with many checkboxes of this kind :

<label class="checkbox">
<input type="checkbox" id="user_ids_for_edit" name="user_ids_for_edit[]" data-toggle="checkbox" data-toggle="checkbox" value="14">Billy Joel
</label>

The form is part of a modal.

When the modal loads, I run an ajax query to check the checkboxes, based on the result of the ajax query :

$.ajax({
   type: "GET",
   url: "/data/team_users",
   data: { id: data.id },
   cache: false,
   dataType: "json",
   success: function(response)
      {
        for (var user in response) {
            alert(response[user].id); // this gives the right user id, so my ajax call works... 
            $('#user_ids_for_edit[value="'+ response[user].id +'"]').attr('checked', true);
        }

      }
});

Unfortunatelly, the relevant checkboxes don't get checked. I don't know how to progress further. Any help would be greatly appreciated.




Aucun commentaire:

Enregistrer un commentaire